1. 程式人生 > >通過HttpWebRequest在後臺對WebService進行呼叫

通過HttpWebRequest在後臺對WebService進行呼叫

目錄:

1  後臺呼叫Webservice的業務需求

2  WebService支援的互動協議

3  如何配置WebService支援的協議

4  後臺對WebService的呼叫

4.1 SOAP 1.1 後臺呼叫例項

4.2 SOAP 1.2 後臺呼叫例項

注:本文章的開發環境為VSS2008  .net FrameWork 3.5

本文章設計到使用的程式碼示例的WebService 為

服務路徑:http://localhost/WebServiceTest/Service1.asmx

服務介面:

[WebMethod]

public string HelloWorld(string StudentName,string

PassWord)

{

return "Hello World";

}

1  後臺呼叫Webservice的業務需求

   在實際開發環境中,我們常常呼叫WebService時,通過專案中引用現實部署的WebService的Asmx檔案,生成客戶端代理類的方式。這種方式將和WebService進行了二次封裝,並以代理類的方式進行呼叫,有利用簡單,快捷的開發。

這種開發方式包含了兩個重要的問題

1)在開發環境中必須可以訪問需要呼叫的WebService,在開發一些大公司的內網系統時,我們往往在開發環境中訪問不到,只僅僅在部署環境中訪問。

2)WebService的介面發生版本變更,我們的應用系統需要重新編譯並部署。

在發現以上的困惑後,直覺告訴我們,我們需要一種直接通過互動協議的方式進行訪問WebService。就像網頁爬蟲一樣,去互動業務操作。

2  WebService支援的互動協議

WebService支援三種方式

1)Http post 方式(注意這種方式只對於本機除錯使用,在web服務部署在其他機器上,應用程式不能通過 Http Post方式呼叫)

       具體互動格式如下:

POST /WebServiceTest/Service1.asmx/HelloWorld HTTP/1.1

Host: localhost

Content-Type: application/x-www-form-urlencoded

Content-Length: length

StudentName=string&PassWord=string

2)SOAP1.1協議 注意Soap協議是基於HTTP的協議,也就是在HTTP的基礎上再次封裝

互動格式如下:

POST /WebServiceTest/Service1.asmx HTTP/1.1

Host: localhost

Content-Type: text/xml; charset=utf-8

Content-Length: length

SOAPAction: "http://tempuri.org/HelloWorld"

<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

  <soap:Body>

    <HelloWorld xmlns="http://tempuri.org/">

      <StudentName>string</StudentName>

      <PassWord>string</PassWord>

    </HelloWorld>

  </soap:Body>

</soap:Envelope>

3)SOAP1.2 協議

互動格式如下:

POST /WebServiceTest/Service1.asmx HTTP/1.1

Host: localhost

Content-Type: application/soap+xml; charset=utf-8

Content-Length: length

<?xml version="1.0" encoding="utf-8"?>

<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">

  <soap12:Body>

    <HelloWorld xmlns="http://tempuri.org/">

      <StudentName>string</StudentName>

      <PassWord>string</PassWord>

    </HelloWorld>

  </soap12:Body>

</soap12:Envelope>

3  如何配置WebService支援的協議

WebService支援的協議包含兩種 Soap1.1 Soap1.2 對於webService 來講可以通過配置檔案配置,支援那些協議,預設的情況下兩種協議都支援。

具體的配置方式為:

在配置檔案中

<webServices>

<protocols>

<add name="HttpSoap1.2"/>

<add name="HttpSoap1.1"/>

</protocols>

</webServices>

4  後臺對WebService的呼叫

4.1 SOAP 1.1 後臺呼叫例項

 string   str1="\"雙引號\"";  

Console.WriteLine("新開始進行連線測試");

string param = @"<?xml version=""1.0"" encoding=""utf-8""?>

<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">

<soap:Body>

<HelloWorld xmlns=""http://tempuri.org/"">

<StudentName>1</StudentName>

<PassWord>1</PassWord>

</HelloWorld>

</soap:Body>

</soap:Envelope>";

 byte[] bs = Encoding.UTF8.GetBytes(param);

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://fox-gaolijun/Short_Message/Service1.asmx");

myRequest.Method = "POST";

myRequest.ContentType = "text/xml; charset=utf-8";

myRequest.Headers.Add("SOAPAction", "http://tempuri.org/HelloWorld");

myRequest.ContentLength = bs.Length;

Console.WriteLine("完成準備工作");

using (Stream reqStream = myRequest.GetRequestStream())

{

reqStream.Write(bs, 0, bs.Length);

}

using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())

{

             StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);

responseString = sr.ReadToEnd();

Console.WriteLine("反饋結果" + responseString);

}  

Console.WriteLine("完成呼叫介面");

}

catch (Exception e)

{

Console.WriteLine(System.DateTime.Now.ToShortTimeString() + "LBS EXCEPTION:" + e.Message);

Console.WriteLine(e.StackTrace);

}

4.1 SOAP 1.2 後臺呼叫例項

Console.WriteLine("新開始進行連線測試");

string responseString;

string param = @"<?xml version=""1.0"" encoding=""utf-8""?>

<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">

<soap12:Body>

<HelloWorld xmlns=""http://tempuri.org/"">

<StudentName>1212</StudentName>

<PassWord>12121</PassWord>

</HelloWorld>

</soap12:Body>

</soap12:Envelope>";

byte[] bs = Encoding.UTF8.GetBytes(param);

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(http://fox-gaolijun/Short_Message/Service1.asmx");

myRequest.Method = "POST";

myRequest.ContentType = "application/soap+xml; charset=utf-8";

myRequest.ContentLength = bs.Length;

Console.WriteLine("完成準備工作");

using (Stream reqStream = myRequest.GetRequestStream())

{

reqStream.Write(bs, 0, bs.Length);

}

using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())

{

StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);

responseString = sr.ReadToEnd();

Console.WriteLine("反饋結果" + responseString);

}

Console.WriteLine("完成呼叫介面");

}

catch (Exception e)

{

Console.WriteLine(System.DateTime.Now.ToShortTimeString() + "LBS EXCEPTION:" + e.Message);

Console.WriteLine(e.StackTrace);

}