原文地址:http://www.javaeye.com/topic/151541
在原文基礎上加了點注釋 呵呵。
所需jar包:
saaj.jar
wsdl4j-1.5.1.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
axis.jar
這些jar包都在axis項目下,
axis下載地址:http://ws.apache.org/axis/
java代碼:
-
- import javax.xml.namespace.QName;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- import org.apache.axis.encoding.XMLType;
-
-
- String url="http://localhost:1246/WebSite3/Service.asmx";
-
- String namespace = "http://tempuri.org/";
-
- String methodName = "HelloWorld";
- String soapActionURI = "http://tempuri.org/HelloWorld";
- Service service = new Service();
-
- Call call = (Call) service.createCall();
-
- call.setTargetEndpointAddress(new java.net.URL(url));
- call.setUseSOAPAction(true);
-
- call.setSOAPActionURI(soapActionURI);
-
- call.setOperationName(new QName(namespace, methodName));
-
- call.addParameter( new QName(namespace,"temp"),
- org.apache.axis.encoding.XMLType.XSD_STRING,
- javax.xml.rpc.ParameterMode.IN);
-
- call.setReturnType(XMLType.XSD_STRING);
-
- String ret = (String) call.invoke(new Object[] { "kusix" });
- System.out.println("返回結(jié)果---> " + ret);
// 需導入的類import javax.xml.namespace.QName;import org.apache.axis.client.Call;import org.apache.axis.client.Service;import org.apache.axis.encoding.XMLType;// .net webService 地址String url="http://localhost:1246/WebSite3/Service.asmx";// .net webService 命名空間String namespace = "http://tempuri.org/";// .net webService 需調(diào)用的方法String methodName = "HelloWorld";String soapActionURI = "http://tempuri.org/HelloWorld";Service service = new Service();Call call = (Call) service.createCall();call.setTargetEndpointAddress(new java.net.URL(url));call.setUseSOAPAction(true);// 這個地方?jīng)]設對就會出現(xiàn)Server was unable to read request的錯誤call.setSOAPActionURI(soapActionURI);// 設置要調(diào)用的.net webService方法call.setOperationName(new QName(namespace, methodName));// 設置該方法的參數(shù),temp為.net webService中的參數(shù)名稱call.addParameter( new QName(namespace,"temp"),org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);// 設置該方法的返回值call.setReturnType(XMLType.XSD_STRING);// call.invoke(new Object[] { "kusix" }); 中"kusix"為傳入?yún)?shù)值String ret = (String) call.invoke(new Object[] { "kusix" });System.out.println("返回結(jié)果---> " + ret);
.net 代碼
- using System;
- using System.Web;
- using System.Web.Services;
- using System.Web.Services.Protocols;
-
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- public class Service : System.Web.Services.WebService
- {
- public Service () {
-
-
-
- }
-
- [WebMethod]
- public string HelloWorld(String temp) {
- return temp+" add by .net";
- }
-
- }