码迷,mamicode.com
首页 > Web开发 > 详细

通过http请求获取指定url的页面

时间:2015-05-27 22:55:38      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:c#   url   html   

 一、定义类
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请求获取指定url的页面

标签:c#   url   html   

原文地址:http://blog.csdn.net/zhenzhenzhao12/article/details/46053095

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!