採用RPC的方式參考博文:Client調用(Axis2-RPC)。
採用的免費webservice接口:
http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
1
客戶端調用代碼如下:
package com.web.hh.constroller;
import java.util.Iterator;
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 org.apache.axiom.soap.SOAP11Constants;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
public class ClientWeather {
/*
* 第二種方式,手動調用
*/
public static void main(String[] args) throws AxisFault {
ServiceClient serviceClient = new ServiceClient();
Options option = new Options();
option.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
option.setTransportInProtocol(Constants.TRANSPORT_HTTP);
option.setAction("http://WebXml.com.cn/getWeather");
// 值爲targetNamespace+methodName
EndpointReference epfs = new EndpointReference("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl");
option.setTo(epfs);
serviceClient.setOptions(option);
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace namespace = fac.createOMNamespace("http://WebXml.com.cn/", "");
OMElement element = fac.createOMElement("getWeather", namespace);
OMElement theCityCode = fac.createOMElement("theCityCode ", namespace);
theCityCode.setText("北京");
element.addChild(theCityCode);
OMElement theUserID = fac.createOMElement("theUserID ", namespace);
theUserID.setText("");
element.addChild(theUserID);
OMElement result = serviceClient.sendReceive(element);
System.out.println(result);
System.out.println("****************************************************************************************************************");
Iterator in = result.getChildrenWithLocalName("getWeatherResult");
while(in.hasNext()){
OMElement om = (OMElement)in.next();
Iterator in2 = om.getChildElements();
while(in2.hasNext()){
// System.out.println(in2.next().toString());
System.out.println(((OMElement)in2.next()).getText());
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
輸出結果如下:
【Tips】上面服務是不需要驗證的,如果需要驗證,參考代碼如下:
/**
* 爲SOAP Header構造驗證信息,
* 如果你的服務端是沒有驗證的,那麼你不用在Header中增加驗證信息
*
* @param serviceClient
* @param tns 命名空間
* @param user
* @param passwrod
*/
public void addValidation(ServiceClient serviceClient, String tns , String user, String passwrod) {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(tns, "nsl");
OMElement header = fac.createOMElement("AuthenticationToken", omNs);
OMElement ome_user = fac.createOMElement("Username", omNs);
OMElement ome_pass = fac.createOMElement("Password", omNs);
ome_user.setText(user);
ome_pass.setText(passwrod);
header.addChild(ome_user);
header.addChild(ome_pass);
serviceClient.addHeader(header);
}
---------------------