C# Get请求数据 WebClient和HttpWebRequest获取HTML代码 蓝色梦想网
#region C# Get请求数据
/// <summary>
/// C# Get请求数据
/// </summary>
/// <param name="url">请求的URL地址</param>
/// <returns></returns>
private static string GetResult(string url)
{
WebClient wc = new WebClient();
string s = wc.DownloadString(url);
s = HttpUtility.UrlDecode(s);
return s;
}
/// <summary>
/// 可返回WEB请求出错请求信息的GET方法
/// </summary>
/// <param name="url">请求的URL</param>
/// <returns></returns>
private static string GetResultByHttpWebRequst(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.UserAgent = "MSIE6.0";
req.Method = "GET";
HttpWebResponse res;
try { res = (HttpWebResponse)req.GetResponse(); }
catch (WebException ex) { res = (HttpWebResponse)ex.Response; }
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
string bstr = sr.ReadToEnd();
return bstr;
}
#endregion
推荐阅读:蓝色梦想网