標籤: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請求並且取迴響應。