over 4 years ago

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

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

引用類別

SOAPClient.cs
System.Net;
System.Xml;
System.Text;

引用端點 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.cs
String endpoint = "http://www.webservicex.net/globalweather.asmx?WSDL";
// 讀取 request_message
XmlDocument doc = new XmlDocument();
doc.Load(@"request_message.xml");
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = doc.OuterXml;
byte[] data = encoding.GetBytes(postData);

// HTTP POST 請求物件
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(endpoint);
httpWReq.Method = "POST";
httpWReq.ContentType = "application/soap+xml; charset=utf-8";
httpWReq.ContentLength = data.Length;
using (Stream stream = httpWReq.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
Console.Write(responseString);

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 網路程式 Web Service SOAP 客戶端 - HTTP Post 版 IHE - 資訊技術基礎建設技術架構書 →
 
comments powered by Disqus