WebClient位於System.Net命名空間下,通過這個類可以方便的創建Http請求並獲取返回內容。
一、用法1 - DownloadData
string uri = "http://hovertree.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);
二、用法2 - OpenRead
string uri = " http://hovertree.com";
WebClient wc = new WebClient();
Console.WriteLine("Sending an HTTP GET request to " + uri);
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);