C#使用SOAP調用Web Service

程序代碼
using System;
using System.IO;
using System.Net;
using System.Text;

namespace ConsoleApplication1
{

    class Program
    {

        static void Main(string[] args)
        {
            //構造soap請求信息
            StringBuilder soap = new StringBuilder();
            soap.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            soap.Append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
            soap.Append("<soap:Body>");
            soap.Append("<getWeather xmlns=\"http://WebXml.com.cn/\">");
            soap.Append("<theCityCode>2210</theCityCode>");
            soap.Append("<theUserID></theUserID>");
            soap.Append("</getWeather>");
            soap.Append("</soap:Body>");
            soap.Append("</soap:Envelope>");

            //發起請求
            Uri uri = new Uri("http://www.webxml.com.cn/WebServices/WeatherWS.asmx");
            WebRequest webRequest = WebRequest.Create(uri);
            webRequest.ContentType = "text/xml; charset=utf-8";
            webRequest.Method = "POST";
            using (Stream requestStream = webRequest.GetRequestStream())
            {
                byte[] paramBytes = Encoding.UTF8.GetBytes(soap.ToString());
                requestStream.Write(paramBytes, 0, paramBytes.Length);
            }

            //響應
            WebResponse webResponse = webRequest.GetResponse();
            using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
            {
                Console.WriteLine(myStreamReader.ReadToEnd());
            }

            Console.ReadKey();
        }
    }

}


返回結果:web

引用內容
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getWeatherResponse xmlns="http://WebXml.com.cn/"><getWeatherResult><string>福建 福州</string><string>福州</string><string>2210</string><string>2010/01/21 15:42:15</string><string>今日天氣實況:氣溫:12.5℃;風向/風力:東南風 4級;溼度:94%;氣壓:1011.9hPa</string><string>空氣質量:良;紫外線強度:暫無</string><string>穿衣指數:建議着薄型套裝或牛仔衫褲等春秋過渡裝。年老體弱者宜着套裝、夾克衫等。
感冒指數:將有一次強降溫過程,且空氣溼度較大,極易發生感冒,請特別注意增長衣服保暖防寒。
晨練指數:有陣雨,較不宜晨練,若堅持室外鍛鍊,請攜帶雨具。建議年老體弱人羣適當減小晨練時間。
洗車指數:不宜洗車,將來24小時內有雨,若是在此期間洗車,雨水和路上的泥水可能會再次弄髒您的愛車。
晾曬指數:偶爾的降雨可能會淋溼晾曬的衣物,不太適宜晾曬。請隨時注意天氣變化。
旅遊指數:有陣雨,溫度適宜,在細雨中游玩別有一番情調,可不要錯過機會呦!但記得出門要攜帶雨具。
路況指數:有小雨,路面潮溼,車輛易打滑,請當心駕駛。
溫馨度指數:白天不太熱也不太冷,風力不大,相信您在這樣的天氣條件下,應會感到比較清爽和溫馨。</string><string>1月21日 陣雨轉小雨</string><string>12℃/19℃</string><string>無持續風向微風</string><string>3.gif</string><string>7.gif</string><string>1月22日 小雨</string><string>9℃/14℃</string><string>無持續風向微風</string><string>7.gif</string><string>7.gif</string><string>1月23日 小雨</string><string>8℃/12℃</string><string>無持續風向微風</string><string>7.gif</string><string>7.gif</string><string>1月24日 陰</string><string>12℃/17℃</string><string>無持續風向微風</string><string>2.gif</string><string>2.gif</string><string>1月25日 多雲</string><string>13℃/18℃</string><string>無持續風向微風</string><string>1.gif</string><string>1.gif</string></getWeatherResult></getWeatherResponse></soap:Body></soap:Envelope>


ASP版

瀏覽器

<%
Dim xmlHttp,soap

soap = soap & "<?xml version=""1.0"" encoding=""utf-8""?>"
soap = soap & "<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">"
soap = soap & "<soap:Body>"
soap = soap & "<getWeather xmlns=""http://WebXml.com.cn/"">"
soap = soap & "<theCityCode>2210</theCityCode>"
soap = soap & "<theUserID></theUserID>"
soap = soap & "</getWeather>"
soap = soap & "</soap:Body>"
soap = soap & "</soap:Envelope>"

Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
xmlHttp.Open "post","http://www.webxml.com.cn/WebServices/WeatherWS.asmx",false
xmlHttp.SetRequestHeader "Content-Type","text/xml; charset=utf-8"
‘曾見過Java的ws要求必定要有SOAPAction,不然報錯no SOAPAction header!
xmlHttp.SetRequestHeader "SOAPAction","http://WebXml.com.cn/getWeather"
xmlHttp.Send(soap)
If xmlHttp.Status = 200 Then
    Response.Write(xmlHttp.responseXML.xml)
End If
Set xmlHttp = Nothing
%>


帶SoapHeader的Web Service調用

post

using System;
using System.IO;
using System.Net;
using System.Text;

namespace ConsoleApplication1
{

    class Program
    {

        static void Main(string[] args)
        {
            //構造soap請求信息
            StringBuilder soap = new StringBuilder();
            soap.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            soap.Append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
            soap.Append("<soap:Header>");
            soap.Append("<MySoapHeader xmlns=\"http://www.mzwu.com/\" soap:mustUnderstand=\"1\">");//mustUnderstand=1,接收者必須能處理此SoapHeader信息,不然返回錯誤
            soap.Append("<UserName>admin</UserName>");
            soap.Append("<UserPass>admin888</UserPass>");
            soap.Append("</MySoapHeader>");
            soap.Append("</soap:Header>");
            soap.Append("<soap:Body>");
            soap.Append("<Hello xmlns=\"http://www.mzwu.com/\">");
            soap.Append("<name>dnawo</name>");
            soap.Append("</Hello>");
            soap.Append("</soap:Body>");
            soap.Append("</soap:Envelope>");

            //發起請求
            Uri uri = new Uri("http://localhost:39674/Service1.asmx");
            WebRequest webRequest = WebRequest.Create(uri);
            webRequest.ContentType = "text/xml; charset=utf-8";
            webRequest.Method = "POST";
            using (Stream requestStream = webRequest.GetRequestStream())
            {
                byte[] paramBytes = Encoding.UTF8.GetBytes(soap.ToString());
                requestStream.Write(paramBytes, 0, paramBytes.Length);
            }

            //響應
            WebResponse webResponse = webRequest.GetResponse();
            using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
            {
                Console.WriteLine(myStreamReader.ReadToEnd());
            }

            Console.ReadKey();
        }
    }

}


小技巧

在瀏覽器中查看Web Service時,有詳細的SOAP請求和響應示例:
技術分享

參考資料

·SOAP教程:http://www.w3school.com.cn/soap/index.asp
·簡單對象訪問協議:http://zh.wikipedia.org/zh-cn/SOAP
·SOAP調用Web Service:http://book.51cto.com/art/200906/129733.htm
·關於朗新WEBSERVER接口問題:http://fpcfjf.blog.163.com/blog/static/5546979320081069223827/ui

相關文章
相關標籤/搜索