一、定义类
public class HttpWebResponseUtility { private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; /// <summary> /// 创建GET方式的HTTP请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="timeout">请求的超时时间</param> /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param> /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param> /// <returns></returns> public static string CreateGetHttpResponse(string url,int? timeout,string userAgent=null,CookieCollection cookies=null) { if(string.IsNullOrEmpty(url)) { throw new ArgumentNullException("url"); } HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = "GET"; request.UserAgent = DefaultUserAgent; if(!string.IsNullOrEmpty(userAgent)) { request.UserAgent = userAgent; } if(timeout.HasValue) { request.Timeout = timeout.Value; } if(cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } HttpWebResponse webreponse = request.GetResponse() as HttpWebResponse; try { using(StreamReader reader = new StreamReader(webreponse.GetResponseStream(),System.Text.Encoding.UTF8)) { return reader.ReadToEnd(); } } catch(Exception exp) { return exp.ToString(); } } }
二、调用方式:
HttpWebResponseUtility.CreateGetHttpResponse(url,null);拿到字符串格式的HTML文本就可以写入新的html中。
原文地址:http://blog.csdn.net/zhenzhenzhao12/article/details/46053095