JAVA调用.NET的WEB SERVICE

来源:互联网 发布:c语言源码是什么 编辑:程序博客网 时间:2024/05/08 00:05
先举两个成功调用的例子:
例1:
/**
* QQ在线状态查询
*/
public static void main(String[] args) {
//设置代理
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", "192.168.0.26");
System.getProperties().put("proxyPort", "808");
//设置socket代码
System.getProperties().put("socksProxySet", "true");
System.getProperties().put("socksProxyHost", "192.168.0.26");
System.getProperties().put("socksProxyPort", "1080");
try {
//WebServices服务地址
String endpoint = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));

//要调用的方法名
call.setOperationName("qqCheckOnline");
//设置调用方法参数
call.addParameter(new javax.xml.namespace.QName("http://WebXml.com.cn/",
"qqCode"), org.apache.axis.encoding.XMLType.XSD_STRING, String.class,
javax.xml.rpc.ParameterMode.IN);

//设置返回类型
call.setReturnClass(String.class);
/**
//new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema"
//, "string")或org.apache.axis.encoding.XMLType.XSD_STRING都可以
//设置返回类型
call.setReturnType(new javax.xml.namespace.QName(
"http://www.w3.org/2001/XMLSchema", "string"));
//返回参数名
call.setReturnQName(new javax.xml.namespace.QName(
"http://WebXml.com.cn/", "qqCheckOnlineResult"));
**/

call.setUseSOAPAction(true);
call.setSOAPActionURI("http://WebXml.com.cn/qqCheckOnline");

//http://WebXml.com.cn/ 命名空间
call.setOperationName(new javax.xml.namespace.QName(
"http://WebXml.com.cn/", "qqCheckOnline"));

String k = (String) call.invoke(new Object[] { "285336157" });
System.out.println("result is " + k.toString() + ".");
} catch (Exception e) {
e.printStackTrace();
}

}
例2:

package MyWebServiceJavaClient;

import java.util.Date;

import java.text.DateFormat;

import java.util.Date;

import java.text.DateFormat;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import javax.xml.namespace.QName;

import java.lang.Integer;

import javax.xml.rpc.ParameterMode;

 

/**

 * <p>Title: </p>

 * <p>Description: </p>

 * <p>Copyright: Copyright (c) 2004</p>

 * <p>Company: </p>

 * @author not attributable

 * @version 1.0

 */

 

public class TestNetService {

  public TestNetService() {

      }

 

      public static void main(String[] args) {

          try {

            Integer i = new Integer(1);

            Integer j = new Integer(2);

            String endpoint="http://localhost/MyServices/WebServiceTest/SumService.asmx";

 

            Service service = new Service();

            Call call = (Call)service.createCall();

 

            call.setTargetEndpointAddress(new java.net.URL(endpoint));

            call.setOperationName(new QName("http://www.my.com/SU","IntAdd"));

 

            call.addParameter("a",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);

            call.addParameter("b",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);

            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);

            call.setUseSOAPAction(true);

            call.setSOAPActionURI("http://www.my.com/Rpc");

 

 

           Integer k = (Integer)call.invoke(new Object[]{i,j});

 

            System.out.println( "result is  "  + k.toString() + ".");

          }

          catch (Exception e) {System.err.println(e.toString());}

        }

 

}

运行上面的java客户端程序,系统会抛出一个SoapAction异常。需要在服务器端SumService.asmx中的 [web method] 的上一行添加下列代码:

[SoapRpcMethod(Action="http://www.my.com/Rpc",RequestNamespace="http://www.my.com/SU",ResponseNamespace="http://www.my.com/SU")] 

不同方法Action不同。

如果用的是eclipse,它自动生成的方法不能直接用,不过似乎jbuild可以。
JAVA调用.NET的WEB SERVICE时客户端需要显式指定与服务器端相同的soapaction.

原创粉丝点击