直接拿例子说事:
这就是我要调的web service地址:
SOAP 1.1
以下是
SOAP 1.2
请求和响应示例。所显示的
占位符
需替换为实际值。
POST /CorpExternalInterface/DomesticFlightService/DomesitFlights.asmx HTTP/1.1
Host: www.corporatetravel.ctrip.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.ctrip.com/webservice/flight/GetOrderInformation"
?
<?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>
<GetOrderInformation xmlns="http://www.ctrip.com/webservice/flight/">
<request xmlns="">
<Authentication>
<UserID>string</UserID>
<Password>string</Password>
<Status>Noramal or Encrypted</Status>
<EmployeeID>string</EmployeeID>
<EmployeeUID>string</EmployeeUID>
</Authentication>
<OrderID>int</OrderID>
<Language>Chinese or English</Language>
</request>
</GetOrderInformation>
</soap:Body>
</soap:Envelope>
后面要做的事就是把以上XML中的占位符填充后POST出去。
编写以下代码:
StringBuffer ret = new StringBuffer("<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//">");
ret.append("<soap:Body>");
ret.append("<GetOrderInformation xmlns=/" http://www.ctrip.com/webservice/flight//">");
ret.append("<request xmlns=/"/">");
ret.append("<Authentication>");
ret.append("<UserID>userid</UserID>");
ret.append("<Password>password</Password>");
ret.append("<Status>Noramal</Status>");
ret.append("<EmployeeID></EmployeeID>");
ret.append("<EmployeeUID></EmployeeUID>");
ret.append("</Authentication>");
ret.append("<OrderID>0000001</OrderID>");
ret.append("<Language>Chinese</Language>");
ret.append("</request>");
ret.append("</GetOrderInformation>");
ret.append("</soap:Body>");
ret.append("</soap:Envelope>");
ret.append("<soap:Body>");
ret.append("<GetOrderInformation xmlns=/" http://www.ctrip.com/webservice/flight//">");
ret.append("<request xmlns=/"/">");
ret.append("<Authentication>");
ret.append("<UserID>userid</UserID>");
ret.append("<Password>password</Password>");
ret.append("<Status>Noramal</Status>");
ret.append("<EmployeeID></EmployeeID>");
ret.append("<EmployeeUID></EmployeeUID>");
ret.append("</Authentication>");
ret.append("<OrderID>0000001</OrderID>");
ret.append("<Language>Chinese</Language>");
ret.append("</request>");
ret.append("</GetOrderInformation>");
ret.append("</soap:Body>");
ret.append("</soap:Envelope>");
byte[] content = ret.toString().getBytes();
URL url = new URL("
https://www.corporatetravel.ctrip.com/CorpExternalInterface/DomesticFlightService/DomesitFlights.asmx");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Host", " www.corporatetravel.ctrip.com") ;
con.setRequestProperty("Content-Type", "text/xml; charset=utf-8") ;
con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
con.setRequestProperty("SOAPAction", "/" http://www.ctrip.com/webservice/flight/GetOrderInformation/"") ;
con.setRequestProperty("Host", " www.corporatetravel.ctrip.com") ;
con.setRequestProperty("Content-Type", "text/xml; charset=utf-8") ;
con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
con.setRequestProperty("SOAPAction", "/" http://www.ctrip.com/webservice/flight/GetOrderInformation/"") ;
con.setRequestProperty("Content-Length", content.length +"");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setDoOutput(true);
con.setUseCaches(false);
con.setAllowUserInteraction(false);
java.io.OutputStream out = con.getOutputStream();
out.write(content);
out.flush();
out.close();
reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
java.io.OutputStream out = con.getOutputStream();
out.write(content);
out.flush();
out.close();
reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
以上代码只用到这些java类:
java.io.InputStreamReader;
java.io.BufferedReader;
java.io.PrintWriter;
java.lang.StringBuffer ;
java.net.URL;
java.net.HttpURLConnection;
java.io.BufferedReader;
java.io.PrintWriter;
java.lang.StringBuffer ;
java.net.URL;
java.net.HttpURLConnection;
从上可见,直接使用http-post机制调用web service是件非常简单方便的事。
最后,推荐个工具来测试web service,名字叫soapui,我目前使用的版本是soapui-2.5-beta2
使用这个工具,只需要输入web service的wsdl地址,就会列出所有支持的方法,并且产生一个request的样例,你只需要改掉XML里面的?号,再按submit按钮就可以演示调用web service,并且把返回的信息在右边显示出来。