国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

通過HttpWebRequest在后臺對WebService進行調用

2019-11-17 03:56:07
字體:
來源:轉載
供稿:網友
  通過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);

            }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 资溪县| 茌平县| 金溪县| 荔波县| 营山县| 乌审旗| 嘉兴市| 巴林左旗| 黄浦区| 江安县| 昭平县| 嘉善县| 陈巴尔虎旗| 青田县| 陕西省| 会泽县| 甘孜县| 威信县| 平利县| 云安县| 明光市| 边坝县| 泰来县| 年辖:市辖区| 南平市| 毕节市| 连山| 邯郸市| 无为县| 政和县| 萝北县| 河津市| 徐州市| 上杭县| 隆尧县| 濮阳市| 临潭县| 玉环县| 南通市| 广饶县| 张家界市|