XML - 管理資料交換/Web 服務
XML - 管理資料交換
|
相關主題
|
參與
|
| 上一章 | 下一章 |
| ← AJAX | XMLHTTP → |
|
學習目標
|
由以下機構贊助 佐治亞大學 特里商學院 管理資訊系統系 |
Web 服務概述
[edit | edit source]Web 服務是一種新型的 Web 應用程式。它們是自包含、自描述的模組化應用程式,可以在 Web 上釋出、定位和呼叫。Web 服務執行函式,這些函式可以是簡單的請求到複雜的業務流程。一旦 Web 服務部署,其他應用程式(以及其他 Web 服務)就可以發現並呼叫已部署的服務。Web 服務使用 XML 來描述請求和響應,並使用 HTTP 作為其網路傳輸。
Web 服務與Web 應用程式之間的主要區別在於協作。Web 應用程式只是使用 Web 協議定位或呼叫的業務應用程式。同樣,Web 服務也透過網路遠端執行計算函式。但是,Web 服務使用網際網路協議,其特定目的是使機器與機器之間的可互操作協調成為可能。
Web 服務已經成為解決分散式計算相關問題的解決方案。分散式計算是指使用多個系統執行函式,而不是讓單個系統執行它。以前在分散式計算中使用的技術,主要是公共物件請求代理體系結構 (CORBA) 和分散式元件物件模型 (DCOM),存在一些侷限性。例如,兩者都沒有實現完全的平臺獨立性或易於透過防火牆傳輸。此外,DCOM 不是供應商獨立的,它是一個微軟產品。
分散式計算標準的一些主要需求是
- 跨平臺支援企業對企業以及內部通訊。
- 儘可能符合現有的網際網路基礎設施。
- 可擴充套件性,包括節點數量和複雜性。
- 國際化。
- 容錯能力。
- 供應商獨立性。
- 適用於微不足道和非微不足道的請求。
隨著時間的推移,商業資訊系統變得高度配置化和差異化。這不可避免地使系統互動變得極其昂貴且耗時。開發人員開始意識到標準化 Web 服務開發的好處。使用 Web 標準似乎是實現這些目標的一個直觀且合乎邏輯的步驟。Web 標準已經為系統通訊提供了一種平臺無關的方式,並且被資訊系統使用者廣泛接受。
最終的結果是 Web 服務的開發。Web 服務形成一個分散式環境,其中可以透過標準化的介面遠端訪問物件。它使用三層模型,定義服務提供者、服務消費者和服務代理。這使得 Web 服務成為一種鬆散的關係,因此如果服務提供者停止執行,代理始終可以將消費者定向到另一個提供者。同樣,也有許多代理,因此消費者始終可以找到可用的代理。為了通訊,Web 服務使用開放的 Web 標準:TCP/IP、HTTP 和基於 XML 的 SOAP。
在更高級別,可能會新增諸如 XAML、XLANG(涉及多個 Web 服務的複雜 Web 事務的交易支援)和 XKMS(Microsoft 和 Verisign 為支援身份驗證和註冊而進行的持續工作)等技術。
SOAP
[edit | edit source]
簡單物件訪問協議 (SOAP)是一種以可擴充套件格式向 Web 服務傳送和接收資訊的方法。SOAP 可以用來發送資訊或遠端過程呼叫,這些呼叫被編碼為 XML。本質上,SOAP 作為一種普遍接受的與 Web 服務通訊的方法。企業遵守 SOAP 約定,以便簡化與 Web 服務互動的過程。
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Header>
<!-- SOAP header -->
</SOAP:Header>
<SOAP:Body SOAP:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<!-- SOAP body -->
</SOAP:Body>
</SOAP:Envelope>
SOAP 訊息包含用於呼叫 Web 服務的請求方法,或者包含對 Web 服務請求的響應資訊。
在開發獨立的 Web 服務時遵守此佈局為企業提供了顯著的優勢。由於 Web 應用程式的設計目的是供眾多參與者使用,因此開發人員希望它們易於採用。使用既定的熟悉標準進行通訊最終減少了使用者有效地與 Web 服務互動所需的努力。
SOAP 信封用於定義和組織 Web 服務訊息中包含的內容。主要的是,SOAP 信封用於指示指定文件將用於服務互動。它包含一個可選的 SOAP 標頭和一個 SOAP 主體。訊息在 SOAP 主體中傳送,SOAP 頭用於傳送主體中不期望的其他資訊。例如,如果 SOAP:actor 屬性存在於 SOAP 頭中,則它表示訊息的接收者應該是誰。
Web 服務事務涉及 SOAP 請求和 SOAP 響應。我們將使用的示例是 Weather.gov 提供的 Web 服務。輸入是緯度、經度、開始日期、所需的天數預報資訊以及資料的格式。SOAP 請求將如下所示
<?xml version="1.0" encoding="UTF-8" standalone="no"?/>
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<m:NDFDgenByDayRequest xmlns:SOAPSDK1="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl">
<latitude xsi:type="xsd:decimal">33.955464</latitude>
<longitude xsi:type="xsd:decimal">-83.383245</longitude>
<startDate xsi:type="xsd:date"></startDate>
<numDays xsi:type="xsd:integer">1</numDays>
<format>24 Hourly</format>
</m:NDFDgenByDayRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
startDate 被留空,因為這將自動獲取最新資料。format 資料型別未定義,因為它在 WSDL 文件中定義。
響應 SOAP 如下所示。
<?xml version="1.0" encoding="UTF-8" standalone="no"?/>
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<NDFDgenByDayResponse xmlns:SOAPSDK1="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl">
<dwmlByDayOut xsi:type="xsd:string">.....</dwmlByDayOut>
</NDFDgenByDayResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOAP 透過在傳送方對資料進行編碼並在接收方對其進行解碼來處理資料。SOAP 處理的資料型別基於 W3C XML Schema 規範。簡單型別包括字串、整數、浮點數和雙精度數,而複合型別由基本型別組成。
<element name="name" type="xsd:string" />
<SOAP:Array SOAP:arrayType="xsd:string[2]">
<string>Web</string>
<string>Services</string>
</SOAP:Array>
由於 SOAP 訊息是基於文字的,因此它們通常不會遇到透過防火牆或其他障礙的任何問題。它們是向 Web 服務傳遞資訊和從 Web 服務接收資訊的理想方式。
服務描述 - WSDL
[edit | edit source]Web 服務描述語言 (WSDL)旨在提供有關如何連線和查詢特定 Web 服務的資訊。該文件也遵守嚴格的格式和組織準則。但是,方法、引數和服務資訊是特定於應用程式的。Web 服務執行不同的功能幷包含獨立的資訊,但它們的組織方式相同。透過為這些服務建立標準的組織架構,開發人員可以有效地呼叫和利用它們,而無需太多或幾乎無需熟悉。要使用 Web 服務,開發人員可以遵循 WSDL 的設計標準,以輕鬆確定與其使用相關的所有資訊和過程。
本質上,WSDL 文件充當與 Web 服務互動的說明。它不包含任何應用程式邏輯,從而為服務提供了一定程度的自主權。這使使用者能夠有效地與服務互動,而不必瞭解其內部工作機制。
以下是一個 WSDL 檔案示例,該檔案用於提供給定美國郵政編碼的溫度的 Web 服務。
<?xml version="1.0"?>
<definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:si="http://soapinterop.org/xsd" xmlns:tns="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl"
xmlns:typens="http://www.weather.gov/forecasts/xml/DWMLgen/schema/DWML.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl>
<types>
<xsd:schema targetNamespace="http://www.weather.gov/forecasts/xml/DWMLgen/schema/DWML.xsd">
<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
<xsd:simpleType name="formatType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="24 hourly" />
<xsd:enumeration value="12 hourly" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="productType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="time-series" />
<xsd:enumeration value="glance" />
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="weatherParametersType">
<xsd:all>
<xsd:element name="maxt" type="xsd:boolean" />
<xsd:element name="mint" type="xsd:boolean" />
<xsd:element name="temp" type="xsd:boolean" />
<xsd:element name="dew" type="xsd:boolean" />
<xsd:element name="pop12" type="xsd:boolean" />
<xsd:element name="qpf" type="xsd:boolean" />
<xsd:element name="sky" type="xsd:boolean" />
<xsd:element name="snow" type="xsd:boolean" />
<xsd:element name="wspd" type="xsd:boolean" />
<xsd:element name="wdir" type="xsd:boolean" />
<xsd:element name="wx" type="xsd:boolean" />
<xsd:element name="waveh" type="xsd:boolean" />
<xsd:element name="icons" type="xsd:boolean" />
<xsd:element name="rh" type="xsd:boolean" />
<xsd:element name="appt" type="xsd:boolean" />
</xsd:all>
</xsd:complexType>
</xsd:schema>
</types>
<message name="NDFDgenRequest">
<part name="latitude" type="xsd:decimal"/>
<part name="longitude" type="xsd:decimal" />
<part name="product" type="typens:productType" />
<part name="startTime" type="xsd:dateTime" />
<part name="endTime" type="xsd:dateTime" />
<part name="weatherParameters" type="typens:weatherParametersType" />
</message>
<message name="NDFDgenResponse">
<part name="dwmlOut" type="xsd:string" />
</message>
<message name="NDFDgenByDayRequest">
<part name="latitude" type="xsd:decimal" />
<part name="longitude" type="xsd:decimal" />
<part name="startDate" type="xsd:date" />
<part name="numDays" type="xsd:integer" />
<part name="format" type="typens:formatType" />
</message>
<message name="NDFDgenByDayResponse">
<part name="dwmlByDayOut" type="xsd:string" />
</message>
<portType name="ndfdXMLPortType">
<operation name="NDFDgen">
<documentation> Returns National Weather Service digital weather forecast data </documentation>
<input message="tns:NDFDgenRequest" />
<output message="tns:NDFDgenResponse" />
</operation>
<operation name="NDFDgenByDay">
<documentation> Returns National Weather Service digital weather forecast data summarized over either 24- or 12-hourly periods </documentation>
<input message="tns:NDFDgenByDayRequest" />
<output message="tns:NDFDgenByDayResponse" />
</operation>
</portType>
<binding name="ndfdXMLBinding" type="tns:ndfdXMLPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="NDFDgen">
<soap:operation soapAction="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl#NDFDgen" style="rpc" />
<input>
<soap:body use="encoded" namespace="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded" namespace="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
<operation name="NDFDgenByDay">
<soap:operation soapAction="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl#NDFDgenByDay" style="rpc" />
<input>
<soap:body use="encoded" namespace="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded" namespace="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
</binding>
<service name="ndfdXML">
<documentation>The service has two exposed functions, NDFDgen and NDFDgenByDay.
For the NDFDgen function, the client needs to provide a latitude and
longitude pair and the product type. The client also needs to provide
the start and end time of the period that it wants data for. For the
time-series product, the client needs to provide an array of boolean values
corresponding to which weather values should appear in the time series product.
For the NDFDgenByDay function, the client needs to provide a latitude and longitude
pair, the date it wants to start retrieving data for and the number of days worth
of data. The client also needs to provide the format that is desired.</documentation>
<port name="ndfdXMLPort" binding="tns:ndfdXMLBinding">
<soap:address location="http://www.weather.gov/forecasts/xml/SOAP_server/ndfdXMLserver.php" />
</port>
</service>
</definitions>
WSDL 檔案定義了一個服務,該服務由不同的端點組成,稱為埠。該埠由網路地址和繫結組成。
<service name="ndfdXML">
<documentation>The service has two exposed functions, NDFDgen and NDFDgenByDay.
For the NDFDgen function, the client needs to provide a latitude and
longitude pair and the product type. The client also needs to provide
the start and end time of the period that it wants data for. For the
time-series product, the client needs to provide an array of boolean values
corresponding to which weather values should appear in the time series product.
For the NDFDgenByDay function, the client needs to provide a latitude and longitude
pair, the date it wants to start retrieving data for and the number of days worth
of data. The client also needs to provide the format that is desired.</documentation>
<port name="ndfdXMLPort" binding="tns:ndfdXMLBinding">
<soap:address location="http://www.weather.gov/forecasts/xml/SOAP_server/ndfdXMLserver.php" />
</port>
</service>
繫結標識每個操作的繫結樣式和協議。在本例中,它使用遠端過程呼叫樣式繫結,使用 SOAP。
<binding name="ndfdXMLBinding" type="tns:ndfdXMLPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="NDFDgen">
<soap:operation soapAction="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl#NDFDgen" style="rpc" />
<input>
<soap:body use="encoded" namespace="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded" namespace="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
<operation name="NDFDgenByDay">
<soap:operation soapAction="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl#NDFDgenByDay" style="rpc" />
<input>
<soap:body use="encoded" namespace="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded" namespace="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
</binding>
埠型別是操作的抽象集合。在本例中,操作是 getTemp。
<portType name="ndfdXMLPortType">
<operation name="NDFDgen">
<documentation> Returns National Weather Service digital weather forecast data </documentation>
<input message="tns:NDFDgenRequest" />
<output message="tns:NDFDgenResponse" />
</operation>
<operation name="NDFDgenByDay">
<documentation> Returns National Weather Service digital weather forecast data summarized over either 24- or 12-hourly periods </documentation>
<input message="tns:NDFDgenByDayRequest" />
<output message="tns:NDFDgenByDayResponse" />
</operation>
</portType>
最後,訊息由操作用於通訊 - 換句話說,用於傳遞引數和返回值。
<message name="NDFDgenByDayRequest">
<part name="latitude" type="xsd:decimal" />
<part name="longitude" type="xsd:decimal" />
<part name="startDate" type="xsd:date" />
<part name="numDays" type="xsd:integer" />
<part name="format" type="typens:formatType" />
</message>
<message name="NDFDgenByDayResponse">
<part name="dwmlByDayOut" type="xsd:string" />
</message>
消費者應該能夠從 WSDL 檔案中訪問 Web 服務中的資料。
有關此特定 Web 服務的更詳細分析,請訪問 Weather.gov
服務發現 - UDDI
[edit | edit source]您已經瞭解瞭如何使用 WSDL 來共享 Web 服務的介面定義,但是您如何找到 Web 服務呢?有無數的獨立 Web 服務由同樣多的不同組織開發和維護。在採用 Web 服務實踐和方法後,開發人員試圖促進他們系統的參與和創造性重用。很快便發現,需要對這些服務及其相應位置進行列舉記錄。此資訊將使開發人員能夠快速輕鬆地利用 Web 服務的最佳實踐和流程。此外,擁有當前 Web 服務功能的中心參考使開發人員能夠避免開發冗餘應用程式。
UDDI 定義了服務釋出和查詢的註冊中心。UDDI 規範由微軟、Ariba 和 IBM 共同建立。UDDI 定義了資料結構和應用程式程式設計介面 (API)。
在前面提到的三層模型中,UDDI 是服務代理。其作用是使服務消費者能夠找到合適的服務提供者。
可以使用 Java API for XML Registries (JAXR) 透過 Java 連線到 UDDI 註冊中心。JAXR 建立了一個抽象層,因此它可以與 UDDI 和其他型別的 XML 註冊中心一起使用,例如 ebXML 註冊中心和儲存庫標準。
要執行 SOAP 訊息,必須使用應用程式與服務提供者進行通訊。由於其靈活性,幾乎任何程式語言都可以用來執行 SOAP 訊息。但是,出於我們的目的,我們將重點關注使用 Java 與 Web 服務進行互動。
使用 Java 與 Web 服務需要一些外部庫。
- Apache SOAP 工具包
- Java 郵件框架
- JavaBeans 啟用框架
- Xerces XML 解析器
讓我們看看如何使用 Java 查詢之前提到的溫度 Web 服務。
import java.io.*;
import java.net.*;
import java.util.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
public class TempClient
{
public static float getTemp (URL url, String zipcode) throws Exception
{
Call call = new Call ();
// Service uses standard SOAP encoding
String encodingStyleURI = Constants.NS_URI_SOAP_ENC;
call.setEncodingStyleURI(encodingStyleURI);
// Set service locator parameters
call.setTargetObjectURI ("urn:xmethods-Temperature");
call.setMethodName ("getTemp");
// Create input parameter vector
Vector params = new Vector ();
params.addElement (new Parameter("zipcode", String.class, zipcode, null));
call.setParams (params);
// Invoke the service ....
Response resp = call.invoke (url,"");
// ... and evaluate the response
if (resp.generatedFault ())
{
throw new Exception();
}
else
{
// Call was successful. Extract response parameter and return result
Parameter result = resp.getReturnValue ();
Float rate=(Float) result.getValue();
return rate.floatValue();
}
}
// Driver to illustrate service invocation
public static void main(String[] args)
{
try
{
URL url=new URL("http://services.xmethods.net:80/soap/servlet/rpcrouter");
String zipcode= "30605";
float temp = getTemp(url,zipcode);
System.out.println(temp);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
這段 Java 程式碼有效地將所有 SOAP 隱藏在使用者面前。它透過名稱和 URL 呼叫目標物件,並設定引數 zipcode。但底層的 SOAP 請求是什麼樣的呢?
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:n="urn:xmethods-Temperature"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<n:getTemp>
<zipcode xsi:type="xs:string">30605</zipcode>
</n:getTemp>
</soap:Body>
</soap:Envelope>
如您所見,SOAP 請求使用 Java 呼叫傳遞的引數來填充 SOAP 信封並引導訊息。同樣,響應作為 '70.0' 返回到 Java 程式。響應 SOAP 也被 Java 程式隱藏了。
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:getTempResponse xmlns:ns1="urn:xmethods-Temperature"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<return xsi:type="xsd:float">70.0</return>
</ns1:getTempResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
這是一個使用 Java 和 SOAP 與 Web 服務互動的附加示例。這個特定的 Web 服務被稱為“美國郵政編碼驗證器”,它以郵政編碼作為引數,然後返回相應的緯度和經度。在開發與 Web 服務互動的應用程式時,第一步應該是檢視 WSDL 文件。
此服務的 WSDL 文件位於此處:http://www.webservicemart.com/uszip.asmx?WSDL
此文件將包含與“美國郵政編碼驗證器”Web 服務互動所需的所有說明。
SOAPClient4XG
修改者 - Duncan McAllister 來自:http://www.ibm.com/developerworks/xml/library/x-soapcl/
import java.io.*;
import java.net.*;
import java.util.*;
public class SOAPClient4XG {
public static void main(String[] args) throws Exception {
args = new String[2];
args[0] = "http://services.xmethods.net:80/soap/servlet/rpcrouter";
args[1] = "SOAPrequest.xml";
if (args.length < 2) {
System.err.println("Usage: java SOAPClient4XG " +
"http://soapURL soapEnvelopefile.xml" +
" [SOAPAction]");
System.err.println("SOAPAction is optional.");
System.exit(1);
}
String SOAPUrl = args[0];
String xmlFile2Send = args[1];
String SOAPAction = "";
// Create the connection where we're going to send the file.
URL url = new URL(SOAPUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;
// Open the input file. After we copy it to a byte array, we can see
// how big it is so that we can set the HTTP Cotent-Length
// property. (See complete e-mail below for more on this.)
FileInputStream fin = new FileInputStream(xmlFile2Send);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
// Copy the SOAP file to the open connection.
copy(fin,bout);
fin.close();
byte[] b = bout.toByteArray();
// Set the appropriate HTTP parameters.
httpConn.setRequestProperty( "Content-Length",
String.valueOf( b.length ) );
httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction",SOAPAction);
httpConn.setRequestMethod( "POST" );
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
// Everything's set up; send the XML that was read in to b.
OutputStream out = httpConn.getOutputStream();
out.write( b );
out.close();
// Read the response and write it to standard out.
InputStreamReader isr =
new InputStreamReader(httpConn.getInputStream());
BufferedReader in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
// copy method from From E.R. Harold's book "Java I/O"
public static void copy(InputStream in, OutputStream out)
throws IOException {
// do not allow other threads to read from the
// input or write to the output while copying is
// taking place
synchronized (in) {
synchronized (out) {
byte[] buffer = new byte[256];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1) break;
out.write(buffer, 0, bytesRead);
}
}
}
}
}
此 Java 類引用了一個 XML 文件 (SOAPRequest.xml),該文件用作 SOAP 訊息。此文件應包含在與呼叫服務的 Java 應用程式相同的專案資料夾中。
在檢視“美國郵政編碼驗證器”WSDL 文件後,很明顯我們希望呼叫“getTemp”方法。此資訊包含在 SOAP 訊息體中,幷包含適當的引數。
SOAPRequest.xml
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:n="urn:xmethods-Temperature"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<n:getTemp>
<zipcode xsi:type="xs:string">30605</zipcode>
</n:getTemp>
</soap:Body>
</soap:Envelope>
在成功互動後,Web 服務提供者將提供一個類似於使用者請求格式的響應。在 NetBeans 中開發時,執行此專案並在 Tomcat 輸出視窗中檢查隨後的 SOAP 訊息響應。
用於此說明的 Netbeans 版本為 5.0。
開啟 Netbeans 後,單擊左側窗格中的“執行時”選項卡,然後右鍵單擊“Web 服務”,選擇“新增 Web 服務”。在“URL”欄位中,輸入 Web 服務 WSDL 檔案的地址,在本例中為“http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl”,然後單擊“獲取 Web 服務描述”。這將顯示 Web 服務的資訊。
| Web 服務是使用 XML 與許多不同系統進行通訊以執行任務的應用程式。為了便於使用 Web 服務,人們開發了協議,使它們能夠靈活且可擴充套件。SOAP 用於傳送和定義資訊,WSDL 用於提供有關如何連線和查詢 Web 服務的資訊。UDDI 描述了這些 Web 服務的查詢位置。 |
- Weather.gov
- WebServices.org
- W3C 的 Web 服務參考
- UDDI.org
- XMethods.net
- Java API for XML Registries
- Apache SOAP 工具包
- Java 郵件框架
- Java 啟用框架
- Xerces Java 解析器
- Jasnowski, Mike. Java、XML 和 Web 服務聖經
- Microsoft Web 服務
- http://www.xml.com/
- https://w3schools.tw/soap/soap_intro.asp
- https://wikibook.tw/wiki/XML_-_Managing_Data_Exchange/Web_services
- http://www.w3.org/TR/2007/REC-soap12-part0-20070427/
- http://www.eweek.com/article2/0,1895,1589730,00.asp
- http://www.ibm.com/developerworks/xml/library/x-soapcl/