axis2 java.net.url_axis,axis2调用.net的webservice
package testClick.src.test;import javax.xml.namespace.QName;import org.apache.axiom.om.OMAbstractFactory;import org.apache.axiom.om.OMElement;import org.apache.axiom.om.OMFactory;import org.apache.axiom.om.OMNamespace;import
今天一個朋友咨詢java調用.net的webservice功能,折騰了2個小時,也都折騰出來了,貼出來,希望用到的朋友少走彎路
1、axis調用.net的webservicepackage 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 {
public 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(new QName("http://WebXml.com.cn/", "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();
}
}
/**
* @param args
*/
public static void main(String[] args) throws Exception{
test();
}
}
2、axis2調用.net的webservice
axis2調用不需要寫那么多,按照下面的步驟,一步一步來,簡單你都想象不到
1、下載axis2(到apache官網下載www.apache.org)
2、我下載的是axis2-1.5-bin.zip,解壓到當前文件夾
3、進入bin目錄(F:\study\java\service\axis2\axis2-1.5\bin)
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;
public class Test {
public static 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();
}
}
/**
* @param args
*/
public static void main(String[] args) throws Exception{
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"
string
用axis2生成java代碼后,Instantaneous Transference Time Limit:?5000MS ? Memory Limit:?65536K Total Submissions:?6367 ? Accepted:?1439 Description It was long ago when we played the game Red Alert. There is a magic function for the game objects which is called inst會自動生成一個對應的對象,webservice需要傳遞的參數,可以通過對這個對象賦值操作完成,如上面,我要查廣州的天氣,就設置為city.setTheCityName("廣州");
注意,關鍵的地方
由于.net webservice中返回的是ArrayOfString,java中沒有這個對象,所以axis2會自動生成這個對象,然后轉換成對應的數組即可,如String[] str = array.getString();在axis版本中,使用的是返回類型,但是返回類型設置其他的比如String等都會報錯,只能設置成VECTOR,即call.setReturnType(XMLType.SOAP_VECTOR),如果只返回一個字符串,可以直接使用STRING;這樣才能確保返回正確。
比較兩個版本,還是覺得axis2使用方便
總結
以上是生活随笔為你收集整理的axis2 java.net.url_axis,axis2调用.net的webservice的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中 普通方法_python中
- 下一篇: php实现贴吧功能,PHPCMS V9万