第0章:簡介
簡單對象訪問協議是交換數據的一種協議規範,是一種輕量的、簡單的、基於XML(標準通用標記語言下的一個子集)的協議,它被設計成在WEB上交換結構化的和固化的信息。webService三要素:SOAP、WSDL (Web Services Description Language)、UDDI( Universal Description Discovery and Integration )之一, soap用來傳遞信息的格式, WSDL 用來描述如何訪問具體的接口, uddi用來管理,分發,查詢webService 具體實現可以搜索 Web Services簡單實例 ; SOAP 可以和現存的許多因特網協議和格式結合使用,包括超文本傳輸協議(HTTP),簡單郵件傳輸協議(SMTP),多用途網際郵件擴充協議(MIME)。它還支持從消息系統到遠程過程調用(RPC)等大量的應用程序。SOAP使用基於XML的數據結構和超文本傳輸協議(HTTP)的組合定義了一個標準的方法來使用Internet上各種不同操作環境中的分佈式對象。(來源:百度百科)
第0節:SOAP消息格式
< SOAP-ENV: Envelope 各種屬性> <!--示例--> < SOAP:HEADER > </ SOAP:HEADER > < SOAP:Body > </ SOAP:Body > </ SOAP-ENV:Envelope > |
構建模塊
-
必需的 Envelope 元素,可把此 XML 文檔標識爲一條 SOAP 消息
-
可選的 Header 元素,包含頭部信息
-
必需的 Body 元素,包含所有的調用和響應信息
-
可選的 Fault 元素,提供有關在處理此消息所發生錯誤的信息
語法規則
-
SOAP 消息必須用 XML 來編碼
-
SOAP 消息必須使用 SOAP Envelope 命名空間
-
SOAP 消息必須使用 SOAP Encoding 命名空間
-
SOAP 消息不能包含 DTD 引用
-
SOAP 消息不能包含 XML 處理指令
例如:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dat="http://dataquery.menergy.com"> <soapenv:Header/> <soapenv:Body> <dat:GetMsg> <!--Optional:--> <dat:req> <!--Optional:--> <dat:id>123</dat:id> <!--Optional:--> <dat:type>123</dat:type> <!--Optional:--> <dat:protocol>123</dat:protocol> <!--Optional:--> <dat:date>20140224</dat:date> <!--Optional:--> <dat:cursor>?</dat:cursor> <!--Optional:--> <dat:pagesize>?</dat:pagesize> <!--Optional:--> <dat:format>34</dat:format> </dat:req> </dat:GetMsg> </soapenv:Body> </soapenv:Envelope> |
第1節:soapui工具
soapui官網:http://www.soapui.org/
開源中國:http://www.oschina.net/p/soapui/
使用博客:http://blog.csdn.net/hailangamy/article/details/6957355
第2節:札記
第1章:實踐
HTTP協議調用SOAP處理類(SoapClientHandler.java):
package com.mcc.core.net;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 使用HTTP協議調用SOAP處理類
*
* @author <a href="mailto:[email protected]">menergy</a>
* DateTime: 14-2-24 下午2:27
*/
public class SoapClientHandler {
/**
* 通過HTTP請求方式調用WebSe
*
* @param soapUrl SOAP的URL,包括wsdl段
* @param soapRequest SOAP的請求
* @return
*/
public static String request(String soapUrl, String soapRequest,String soapAction) {
String actual = "";
HttpURLConnection connection = null;
OutputStream os = null;
PrintWriter out = null;
InputStream is = null;
BufferedReader br = null;
try {
URL url = new URL(soapUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
// 處理請求參數
os = connection.getOutputStream();
out = new PrintWriter(os);
out.println(soapRequest);
// 發送請求
out.flush();
// http status ok
if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
System.out.println("HTTP_OK");
}
// 處理返回信息
is = connection.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
StringBuilder builder = new StringBuilder();
for (String line = br.readLine(); line != null; line = br.readLine()) {
builder.append(line);
}
actual = builder.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 釋放資源
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
if (is != null) {
is.close();
}
if (br != null) {
br.close();
}
if (out != null) {
out.close();
}
if (connection != null) {
connection.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return actual;
}
public static void main(String args[]){
//webservice服務路徑,一般爲wsdl後綴
String serviceUrl = "http://192.168.1.100:8181?wsdl";
StringBuilder request = new StringBuilder();
//下面格式可以從soapui客戶端工具獲得
request.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:dat=\"http://dataquery.menergy.com\">");
request.append("<soapenv:Header/><soapenv:Body>");
request.append("<dat:GetInfo><dat:req>");
request.append("<dat:id>123</dat:id>");
request.append("<dat:type>123</dat:type>");
request.append("<dat:protocol>123</dat:protocol>");
request.append("<dat:date>20140224</dat:date>");
request.append("<dat:cursor></dat:cursor>");
request.append("<dat:pagesize></dat:pagesize>");
request.append("<dat:format>34</dat:format>");
request.append(" </dat:req></dat:GetInfo>");
request.append("</soapenv:Body></soapenv:Envelope>");
// 組裝SOAP請求的參數
String soapRequest = request.toString();
// SOAP ACTION ,對應xmlns命名空間方法路徑
String soapAction = "http://dataquery.menergy.com/GetInfo";
// 使用HTTP協議調用SOAP的數據
String response = SoapClientHandler.request(serviceUrl, soapRequest, soapAction);
//web service 返回結果
System.out.println("響應結果:\n" + response);
}
}