千家信息网

软件使用Java客户端类调用C# WebService

发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,这篇文章给大家分享的是有关软件使用Java客户端类调用C# WebService的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。使用这个类不用安装任何第三方工具,因为采用ht
千家信息网最后更新 2026年11月04日软件使用Java客户端类调用C# WebService

这篇文章给大家分享的是有关软件使用Java客户端类调用C# WebService的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

使用这个类不用安装任何第三方工具,因为采用http的方式发送xml文件,所以你只需要安装好JDK就可以了。执行此类还可以获得WebServices或xml rpc server返回的xml字符流,你可以根据返回的xml数据来进行其他程序处理。通过这种方式实现了Java平台和.NET平台的数据交换和Java客户端类调用C# WebService。

下面是满足Java客户端类调用的源代码SOAPClient4XG.java:

/**   * SOAPClient4XG. Read the SOAP envelope   file passed as the second * parameter, pass it to the SOAP endpoint   passed as the first parameter, and    * print out the SOAP envelope passed   as a response. with help from Michael  * Brennan 03/09/01  *   *  * @author Bob DuCharme  * @version 1.1  * @param SOAPUrl URL of SOAP Endpoint   to send request.  * @param xmlFile2Send A file with an XML   document of the request.   *  * 5/23/01 revision: SOAPAction added  */   import java.io.*;  import java.net.*;   public class SOAPClient4XG {  public static void main(String[] args)   throws Exception {   if (args.length < 2) { //小于  System.err.println("Usage: java SOAPClient4XG " +  "http://soapURL soapEnvelopefile.xml" +  " [SOAPAction]");  System.err.println("SOAPAction is optional.");  System.exit(1);  }   String SOAPUrl = args[0];   String xmlFile2Send = args[1];   String SOAPAction = "";  if (args.length > 2) //大于  SOAPAction = args[2];   // Create the connection where we're going   to send the file.  URL url = new URL(SOAPUrl);  URLConnection connection =   url.openConnection();  HttpURLConnection httpConn =   (HttpURLConnection) connection;   // Open the input file. After we copy   it to a byte array, we can see  // how big it is so that we can set the   HTTP Cotent-Length  // property. (See complete e-mail below   for more on this.)   FileInputStream fin =   new FileInputStream(xmlFile2Send);   ByteArrayOutputStream bout =   new ByteArrayOutputStream();   // Copy the SOAP file to the open connection.  copy(fin,bout);   fin.close();   byte[] b = bout.toByteArray();   // Set the appropriate HTTP parameters.  httpConn.setRequestProperty( "Content-Length",  String.valueOf( b.length ) );  httpConn.setRequestProperty("Content-Type","  text/xml; charset=utf-8");  httpConn.setRequestProperty("SOAPAction",SOAPAction);  httpConn.setRequestMethod( "POST" );  httpConn.setDoOutput(true);  httpConn.setDoInput(true);   // Everything's set up; send the XML   that was read in to b.  OutputStream out = httpConn.getOutputStream();  out.write( b );   out.close();   // Read the response and write it   to standard out.   InputStreamReader isr =  new InputStreamReader(httpConn.getInputStream());  BufferedReader in = new BufferedReader(isr);    String inputLine;   while ((inputLine = in.readLine()) != null)  System.out.println(inputLine);  in.close();  }   // copy method from From E.R. Harold's   book "Java I/O" public static void copy(InputStream in,   OutputStream out)   throws IOException {   // do not allow other threads to read from the  // input or write to the output while copying is // taking place   synchronized (in) {  synchronized (out) {   byte[] buffer = new byte[256];  while (true) {  int bytesRead = in.read(buffer);  if (bytesRead == -1) break;  out.write(buffer, 0, bytesRead);  }  }  }  }   }

编译:javac SOAPClient4XG.java

运行的命令格式: java -classpath . SOAPClient4XG

http://localhost/BokeServices/Service1.asmx c:loginReq.xml

http://tempuri.org/UserLoginReq

不过先不要运行上面的命令,先介绍一下命令行的意思,http://localhost/BokeServices/Service1.asmx是C# WebService的地址;

c:loginReq..xml里的内容是Java客户端类调用WebService方法的xml文件, http://tempuri.org是WebService方法的命名空间,一定要有,否则调用失败,如果你在C# WebServices中使用了方法默认的命名空间的话,就使用http://tempuri.org,否则要与C#中定义的一致,UserLoginReq是C# WebServices的方法名。注意xml文件中的方法名和参数名是与C# WebServices的方法名、参数名是一一对应的(参数顺序是可以颠倒的)。

我先介绍一个简单的例子(c:loginReq.xml),这个xml文件调用了远程C# WebService的UserLoginReq方法,并带UserAcc(用户名)和UserPwd(口令)两个参数,调用成功后C#会自动返回一个xml格式的SOAP包。

〈?xml version="1.0" encoding="utf-8"?〉  〈soap:Envelope xmlns:xsi="  http://www.w3.org/2001/XMLSchema-instance"   xmlns:xsd="http://www.w3.org/2001/XMLSchema"   xmlns:soap="http://schemas.xmlsoap.org/soap/  envelope/"〉  〈soap:Body〉  〈UserLoginReq xmlns="http://tempuri.org/"〉  〈UserAcc〉baozheng〈/UserAcc〉  〈UserPwd〉mypwd〈/UserPwd〉   〈/UserLoginReq〉  〈/soap:Body〉  〈/soap:Envelope〉

现在看一下C# WebServices的UserLoginReq的方法的定义:

public struct UserLoginResp  {  public string UserAcc;  public int Result;  }  [WebMethod]   public UserLoginResp UserLoginReq(string UserAcc,  string UserPwd,int ReqFrom)  {  …  }

注意结构UserLoginResp,C# WebServices返回SOAP信息时会自动将UserLoginResp结构转换成xml的格式。

用此类做xml rpc server 的客户端也很简单,下文是一个客户端rpc.xml文件,调用了xml rpc server 端实现的metaWeblog.deletePost方法。

〈?xml version="1.0" encoding="utf-8"?〉  〈methodCall〉  〈methodName〉metaWeblog.deletePost〈/methodName〉  〈params〉  〈param〉〈value〉appKeyValue〈/value〉〈/param〉  〈param〉〈value〉746〈/value〉〈/param〉   〈param〉〈value〉baozheng〈/value〉〈/param〉  〈param〉〈value〉Hello123〈/value〉〈/param〉  〈/params〉    〈/methodCall〉

调用命令的格式:

java -classpath %CLASSPATH%;. SOAPClient4XG.

http://192.168.25.97:8080/BokeeXmlRpc/xml-rpc rpc.xml

对比调用C# WebServices的命令行,可以看出调用xml rpc server的命令行少一个方法名参数。http://192.168.25.97:8080/BokeeXmlRpc/xml-rpc 是提供xml rpc 调用的server端servlet地址。

感谢各位的阅读!关于"软件使用Java客户端类调用C# WebService"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

C# 方法 客户 客户端 命令 参数 文件 格式 内容 软件 软件使用 地址 平台 数据 方式 更多 空间 篇文章 结构 utf-8 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技工作委员会主任 苏州找大状互联网科技有限公司 互联网科技公司华为 科技赋能下的互联网金融 深圳恒合互联网络科技有限公司 科技互联网龙头 厦门中拓互联网络科技有限公司 互联网科技英文 期待互联网科技 鑫亿盛互联网科技 天津摩尔互联网科技 北京华百购互联网科技有限公司 河南猎农互联网科技有限公司 花神(南京)互联网科技有限公司 江阴互联网智能科技产品进口 注册互联网科技公司经营范围大全 长沙茂晟互联网科技有限公司 美国互联网科技实力 伍佰万互联网科技怎么赚钱 亚光科技低轨卫星互联网 上海睿民互联网科技有限公司 上游互联网科技 互联网科技的未来发展 互联网养老 桔豆科技 无锡溥仪互联网科技有限公司 博兴的互联网电子科技公司 互联网科技公司需要的职位 厦门比较好的科技互联网公司 科技互联网大厂 广州梦谷互联网科技有限公司领导
0