您的位置:首页 > 理论基础 > 计算机网络

C# Get请求数据 WebClient和HttpWebRequest获取HTML代码 蓝色梦想网

2012-06-13 13:44 861 查看
#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

推荐阅读:蓝色梦想网
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐