今天一个朋友咨询java调用.net的webservice功能,折腾了2个小时,也都折腾出来了,贴出来,希望用到的朋友少走弯路?
1、axis调用.net的webservice?
- package?test;??
- ??
- import?java.net.URL;??
- import?javax.xml.namespace.QName;??
- import?org.apache.axis.client.Call;??
- import?org.apache.axis.client.Service;??
- import?org.apache.axis.encoding.XMLType;??
- import?javax.xml.rpc.ParameterMode;??
- public?class?Test?{??
- ????static?void?test()?throws?Exception{??
- ????????Service?service?=?new?Service();??
- ????????Call?call?=?null;??
- ??????????try?{??
- ??????????????call?=?(Call)?service.createCall();??
- ??????????????call.setTargetEndpointAddress(new?URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"));??
- ??????????????call.setOperationName(new?QName("http://WebXml.com.cn/","getWeatherbyCityName"));??
- ??????????????call.addParameter("theCityName"),XMLType.SOAP_VECTOR,ParameterMode.IN);??
- ??????????????call.setReturnType(XMLType.SOAP_VECTOR);??
- ??????????????call.setUseSOAPAction(true);??
- ??????????????call.setSOAPActionURI("http://WebXml.com.cn/getWeatherbyCityName");??
- ??????????????System.out.println(call.invoke(new?Object[]{"广州"}));??
- ??????????}?catch?(Exception?e)?{??
- ??????????????e.printStackTrace();??
- ??????????}??
- ????}??
- ??????
- ?????
- ?
- ?????*/??
- void?main(String[]?args)? ????????test();??
- }??
2、axis2调用.net的webservice?
?? axis2调用不需要写那么多,按照下面的步骤,一步一步来,简单你都想象不到?
?? 1、下载axis2(到apache官网下载www.apache.org)?
?? 2、我下载的是axis2-1.5-bin.zip,解压到当前文件夹?
?? 3、进入bin目录(F:studyjavaserviceaxis2axis2-1.5bin)?
?? 4、打开cmd,进入第3步的bin目录,输入wsdl2java.bat -uri http://www.webxml.c?
om.cn/WebServices/WeatherWebService.asmx?wsdl,回车?
?? 5、之后会在bin目录下生成一个src目录,将src目录下的两个类考到eclipse开发目录下?
?? 6、建一个测试类Test,代码如下?
import?cn.com.webxml.WeatherWebServiceStub;??
import?cn.com.webxml.WeatherWebServiceStub.ArrayOfString;??
import?cn.com.webxml.WeatherWebServiceStub.GetWeatherbyCityName;??
void?test1(){??
????????try{??
????????????WeatherWebServiceStub?stub?=?new?WeatherWebServiceStub();??
????????????stub._getServiceClient().getOptions().setProperty(????
????????????????????org.apache.axis2.transport.http.HTTPConstants.CHUNKED,????
????????????????????Boolean.FALSE);??
????????????GetWeatherbyCityName?city?=?new?GetWeatherbyCityName();??
????????????city.setTheCityName("广州");??
????????????ArrayOfString?array?=?stub.getWeatherbyCityName(city).getGetWeatherbyCityNameResult();??
????????????String[]?str?=?array.getString();??
????????????for(String?s?:?str){??
????????????????System.out.println(s);??
????????????}??
????????}catch(Exception?e){??
????????????e.printStackTrace();??
????????}??
????????test1();??
需要注意的是这个类
GetWeatherbyCityName
,这个本来是.net webservice中的一个方法,如下?
POST?/WebServices/WeatherWebService.asmx?HTTP/1.1??
Host:?www.webxml.com.cn??
Content-Type:?text/xml;?charset=utf-8??
Content-Length:?length??
SOAPAction:?"http://WebXml.com.cn/getWeatherbyCityName"??
<?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>??
????<getWeatherbyCityName?xmlns="http://WebXml.com.cn/">??
??????<theCityName>string</theCityName>??
????</getWeatherbyCityName>??
??</soap:Body>??
</soap:Envelope>??