發送一個簡單的HTTP GET請求並且取迴響應。

來源:互聯網
上載者:User

標籤:blog   http   os   ar   使用   sp   div   on   log   

string uri="http//www.baidu.com";WebClient wc = new WebClient();Console.WriteLine("Sending an HTTP GET request to"+uri);byte[] bResponse = wc.DownloadData(uri);string strResponse = Encoding.ASCII.GetString(bResponse);Console.WriteLine("HTTP response is:");Console.WriteLine(strResponse);
註解:

         WebClient類是System.Net命名空間的一部分,預設情況下對於控制台程式是可訪問的。通過WebClient.DownloadData()方法來取回一個HTTP響應式極為簡單的,但是DownLoadData()返回的只是一個字元型數組(byte array),必須要通過System.Text.Encoding.ASCII.GetString()方法把它轉換成一個字串。另一種方案是使用WebClient.OpenRead()方法並且把它與一個stream相關聯:

string uri ="http://www.baidu.com";WebClient wc = new WebClient();Console.WriteLine("Sending an HTTP GET request to"+url);Stream st = wc.OpenRead(uri);StreamReader sr=new StreamReader(st);string res =sr.ReadToEnd();sr.Close();st.Close();Console.WriteLine("HTTP Response is");Console.WriteLine(res);

    相比於測試ASP.NET Web應用程式,WebClient類更適合用來測試靜態HTML網頁。這段代碼可以用來檢測ASP.NET程式發回的響應,但是要把這段代碼擴充成自動化測試程式,你需要根據期望值來檢查HTTP響應。通過編程的方式決定ASP.NET Web應用程式的ViewState值。如何根據給定的期望值來檢查HTTP響應。

發送一個簡單的HTTP GET請求並且取迴響應。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.