麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

通過HttpWebRequest在后臺對WebService進行調(diào)用

2019-11-17 03:56:07
字體:
供稿:網(wǎng)友
  通過HttpWebRequest在后臺對WebService進行調(diào)用



目錄:

1  后臺調(diào)用Webservice的業(yè)務(wù)需求

2  WebService支持的交互協(xié)議

3  如何配置WebService支持的協(xié)議

4  后臺對WebService的調(diào)用

4.1 SOAP 1.1 后臺調(diào)用實例

4.2 SOAP 1.2 后臺調(diào)用實例



注:本文章的開發(fā)環(huán)境為VSS2008  .net FrameWork 3.5



本文章設(shè)計到使用的代碼示例的WebService 為

服務(wù)路徑:http://localhost/WebServiceTest/Service1.asmx

服務(wù)接口:

[WebMethod]

        public string HelloWorld(string StudentName,string PassWord)

        {

            return "Hello World";

        }



1  后臺調(diào)用Webservice的業(yè)務(wù)需求



   在實際開發(fā)環(huán)境中,我們常常調(diào)用WebService時,通過項目中引用現(xiàn)實部署的WebService的Asmx文件,生成客戶端代理類的方式。這種方式將和WebService進行了二次封裝,并以代理類的方式進行調(diào)用,有利用簡單,快捷的開發(fā)。

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

1) 在開發(fā)環(huán)境中必須可以訪問需要調(diào)用的WebService,在開發(fā)一些大公司的內(nèi)網(wǎng)系統(tǒng)時,我們往往在開發(fā)環(huán)境中訪問不到,只僅僅在部署環(huán)境中訪問。

2)WebService的接口發(fā)生版本變更,我們的應(yīng)用系統(tǒng)需要重新編譯并部署。

    

在發(fā)現(xiàn)以上的困惑后,直覺告訴我們,我們需要一種直接通過交互協(xié)議的方式進行訪問WebService。就像網(wǎng)頁爬蟲一樣,去交互業(yè)務(wù)操作。





2  WebService支持的交互協(xié)議



  

WebService支持 三種方式

1)Http post 方式(注意這種方式只對于本機調(diào)試使用,在web服務(wù)部署在其他機器上,應(yīng)用程序不能通過 Http Post方式調(diào)用)

       具體交互格式如下:

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協(xié)議  注意Soap協(xié)議是基于HTTP的協(xié)議,也就是在HTTP的基礎(chǔ)上再次封裝

交互格式如下:

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 協(xié)議

交互格式如下:

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支持的協(xié)議



WebService支持的協(xié)議 包含兩種 Soap1.1 Soap1.2 對于webService 來講可以通過配置文件配置,支持那些協(xié)議,默認(rèn)的情況下 兩種協(xié)議都支持。

具體的配置方式為:

在配置文件中

<webServices>

  <PRotocols>

    <add name="HttpSoap1.2"/>

    <add name="HttpSoap1.1"/>

</protocols>

</webServices>





4  后臺對WebService的調(diào)用



4.1 SOAP 1.1 后臺調(diào)用實例



                  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("完成準(zhǔn)備工作");

                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("反饋結(jié)果" + responseString);

                }   

                Console.WriteLine("完成調(diào)用接口");



            }

            catch (Exception e)

            {

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



                Console.WriteLine(e.StackTrace);

            }



4.1 SOAP 1.2 后臺調(diào)用實例



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("完成準(zhǔn)備工作");

                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("反饋結(jié)果" + responseString);

                }

                Console.WriteLine("完成調(diào)用接口");



            }

            catch (Exception e)

            {

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



                Console.WriteLine(e.StackTrace);

            }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 一级毛片真人免费播放视频 | 成人一区三区 | 久久精品欧美电影 | 精品亚洲福利一区二区 | 国产韩国精品一区二区三区久久 | 色柚视频网站ww色 | 国产亚洲精品久久 | 国产精品成人一区二区三区电影毛片 | 成人毛片免费播放 | 久久久青 | 91丨九色丨国产在线观看 | 欧美中文字幕一区二区三区亚洲 | 中文字幕一区在线观看视频 | 一道本不卡一区 | 欧美成人免费 | 国产精品久久久久久久久久iiiii | 激情在线视频 | 日韩色视频在线观看 | 在线成人www免费观看视频 | 欧美高清一级片 | 国产亚洲精品yxsp | 一级毛片真人免费播放视频 | 国产一精品久久99无吗一高潮 | 视频一区 日韩 | 日日操夜夜透 | 亚洲天堂岛国片 | 一区二区久久精品66国产精品 | 国产精品久久久久久婷婷天堂 | 免费观看一级 | 在线看免费观看日本 | 日本免费不卡一区二区 | 精品国产一区二区三区四区阿崩 | 亚洲视屏在线观看 | 精品成人国产在线观看男人呻吟 | 精品在线免费播放 | 久久久久久久一区二区三区 | 欧美日韩中文字幕在线 | 伦一区二区三区中文字幕v亚洲 | 草草久久久 | 毛片网站网址 | 激情视频免费观看 |