about 4 years ago

以下實做 HTTP POST 呼叫 Web Service 客戶端程式,以天氣服務為例

透過 Web Service 技術取得天氣資訊

下載 jar

引用類別

SOAPClient.java
org.apache.commons.httpclient.HttpClient
org.apache.commons.httpclient.methods.FileRequestEntity
org.apache.commons.httpclient.methods.PostMethod
org.apache.commons.httpclient.methods.RequestEntity

引用端點 Endpoint

天氣 WebService WSDL

Request Header

  • SOAPAction : http://www.webserviceX.NET/GetWeather
  • Content-Type : application/soap+xml; charset=utf-8"

Request Message

request_message.xml
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
        <wsa:To>http://www.webservicex.net/globalweather.asmx?WSDL</wsa:To>
        <wsa:MessageID>urn:uuid:e475e8e5-6752-4270-a795-781e599fc2ee</wsa:MessageID>
        <wsa:Action>http://www.webserviceX.NET/GetWeather</wsa:Action>
    </soapenv:Header>
    <soapenv:Body xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
        <GetWeather xmlns="http://www.webserviceX.NET">
            <CityName>Taipei</CityName>
            <CountryName>Taiwan</CountryName>
        </GetWeather>
    </soapenv:Body>
</soapenv:Envelope>

客戶端請求

SOAPClient.java
String endpoint = "http://www.webservicex.net/globalweather.asmx?WSDL";

  // 讀取 request_message
File file = new File("request_message.xml");
RequestEntity entity = new FileRequestEntity(file, "text/xml; charset=utf-8");
  
  // HTTP POST 請求物件
PostMethod post = new PostMethod(endpoint);
  post.setRequestEntity(entity);
post.setRequestHeader("SOAPAction", SOAP_ACTION);
post.setRequestHeader("Content-type", "application/soap+xml; charset=utf-8");
post.setRequestHeader("Content-Length", entity.getContentLength() + "");
HttpClient httpclient = new HttpClient();
try {
    int result = httpclient.executeMethod(post);
    System.out.println("Response status code: " + result);
    System.out.println("Response body: ");
    System.out.println(post.getResponseBodyAsString());
} finally {
    post.releaseConnection();
}

Response Message

response_message.xml
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetWeatherResponse xmlns="http://www.webserviceX.NET">
            <GetWeatherResult>
                <?xml version="1.0" encoding="utf-16"?>
                <CurrentWeather>
                    <Location>Sungshan / Taipei, Taiwan (RCSS) 25-04N 121-33E</Location>
                    <Time>Mar 25, 2014 - 12:00 PM EDT / 2014.03.25 1600 UTC</Time>
                    <Wind> from the E (100 degrees) at 5 MPH (4 KT):0</Wind>
                    <Visibility> 5 mile(s):0</Visibility>
                    <SkyConditions> mostly cloudy</SkyConditions>
                    <Temperature> 71 F (22 C)</Temperature>
                    <DewPoint> 62 F (17 C)</DewPoint>
                    <RelativeHumidity> 73%</RelativeHumidity>
                    <Pressure> 29.94 in. Hg (1014 hPa)</Pressure>
                    <Status>Success</Status>
                </CurrentWeather>
            </GetWeatherResult>
        </GetWeatherResponse>
    </soap:Body>
</soap:Envelope>

application/xop+xml;

← Java - 網路程式 Socket C# 網路程式 Web Service SOAP 客戶端 - HTTP Post 版 →
 
comments powered by Disqus