Exemplo n.º 1
0
 /// <summary>
 /// 根据相传入的数据,得到相应页面数据
 /// </summary>
 /// <param name="item">参数类对象</param>
 /// <returns>返回HttpResult类型</returns>
 public HttpResult GetHtml(HttpItem item)
 {
     //返回参数
     HttpResult result = new HttpResult();
     try
     {
         //准备参数
         SetRequest(item);
     }
     catch (Exception ex)
     {
         result.Cookie = string.Empty;
         result.Header = null;
         result.Html = ex.Message;
         result.StatusDescription = "配置参数时出错:" + ex.Message;
         //配置参数时出错
         return result;
     }
     try
     {
         //请求数据
         using (response = (HttpWebResponse)request.GetResponse())
         {
             GetData(item, result);
         }
     }
     catch (WebException ex)
     {
         if (ex.Response != null)
         {
             using (response = (HttpWebResponse)ex.Response)
             {
                 GetData(item, result);
             }
         }
         else
         {
             result.Html = ex.Message;
         }
     }
     catch (Exception ex)
     {
         result.Html = ex.Message;
     }
     if (item.IsToLower) result.Html = result.Html.ToLower();
     return result;
 }
Exemplo n.º 2
0
        protected string Test(string ip)
        {
            // ip = ip.Replace(":", ":");
            HttpHelper http = new HttpHelper();
            ServicePointManager.DefaultConnectionLimit = 1000;
            HttpItem item = new HttpItem()
            {
                URL = "https://www.taobao.com/",//URL     必需项
                Encoding = null,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
                Method = "get",//URL     可选项 默认为Get
                Timeout = 250000,//连接超时时间     可选项默认为100000
                ReadWriteTimeout = 30000,//写入Post返回结果超时时间     可选项默认为30000
                KeepAlive = false,
                ProtocolVersion = HttpVersion.Version10,
                IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写
                //Cookie = "",//字符串Cookie     可选项
                //UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统     可选项有默认值
                //Accept = "text/html, application/xhtml+xml, */*",//    可选项有默认值
                ContentType = "text/html",//返回类型    可选项有默认值
                Referer = "http://www.ip.cn/",//来源URL     可选项
                CerPath = "taobao.cer",
                ResultType = ResultType.Byte ,
                //Allowautoredirect = true,//是否根据301跳转     可选项
                Connectionlimit = 1024,//最大连接数     可选项 默认为1024
                ProxyIp = ip,//代理服务器ID 端口可以直接加到后面以:分开就行了    可选项 不需要代理 时可以不设置这三个参数
               // ResultType = ResultType.String,//返回返回结果类型,是Byte还是String
                CookieCollection = new System.Net.CookieCollection(),//可以直接传一个Cookie集合进来
            };
            //item.Header.Add("测试Key1", "测试Value1");
            //item.Header.Add("测试Key2", "测试Value2");
            //得到HTML代码
            HttpResult result = http.GetHtml(item);
            //取出返回的Cookie
            string cookie = result.Cookie;
            //返回的Html内容
            string html = result.Html;
            if (result.StatusCode == System.Net.HttpStatusCode.OK)
            {

            }
            //表示StatusCode的文字说明与描述
            string statusCodeDescription = result.StatusDescription;
            return html;
        }
Exemplo n.º 3
0
 /// <summary>
 /// 设置Post数据
 /// </summary>
 /// <param name="item">Http参数</param>
 private void SetPostData(HttpItem item)
 {
     //验证在得到结果时是否有传入数据
     if (!request.Method.Trim().ToLower().Contains("get"))
     {
         if (item.PostEncoding != null)
         {
             postencoding = item.PostEncoding;
         }
         byte[] buffer = null;
         //写入Byte类型
         if (item.PostDataType == PostDataType.Byte && item.PostdataByte != null && item.PostdataByte.Length > 0)
         {
             //验证在得到结果时是否有传入数据
             buffer = item.PostdataByte;
         }//写入文件
         else if (item.PostDataType == PostDataType.FilePath && !string.IsNullOrEmpty(item.Postdata))
         {
             StreamReader r = new StreamReader(item.Postdata, postencoding);
             buffer = postencoding.GetBytes(r.ReadToEnd());
             r.Close();
         } //写入字符串
         else if (!string.IsNullOrEmpty(item.Postdata))
         {
             buffer = postencoding.GetBytes(item.Postdata);
         }
         if (buffer != null)
         {
             request.ContentLength = buffer.Length;
             request.GetRequestStream().Write(buffer, 0, buffer.Length);
         }
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// 设置编码
 /// </summary>
 /// <param name="item">HttpItem</param>
 /// <param name="result">HttpResult</param>
 /// <param name="ResponseByte">byte[]</param>
 private void SetEncoding(HttpItem item, HttpResult result, byte[] ResponseByte)
 {
     //是否返回Byte类型数据
     if (item.ResultType == ResultType.Byte) result.ResultByte = ResponseByte;
     //从这里开始我们要无视编码了
     if (encoding == null)
     {
         Match meta = Regex.Match(Encoding.Default.GetString(ResponseByte), "<meta[^<]*charset=([^<]*)[\"']", RegexOptions.IgnoreCase);
         string c = string.Empty;
         if (meta != null && meta.Groups.Count > 0)
         {
             c = meta.Groups[1].Value.ToLower().Trim();
         }
         if (c.Length > 2)
         {
             try
             {
                 encoding = Encoding.GetEncoding(c.Replace("\"", string.Empty).Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk").Trim());
             }
             catch
             {
                 if (string.IsNullOrEmpty(response.CharacterSet))
                 {
                     encoding = Encoding.UTF8;
                 }
                 else
                 {
                     encoding = Encoding.GetEncoding(response.CharacterSet);
                 }
             }
         }
         else
         {
             if (string.IsNullOrEmpty(response.CharacterSet))
             {
                 encoding = Encoding.UTF8;
             }
             else
             {
                 encoding = Encoding.GetEncoding(response.CharacterSet);
             }
         }
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// 设置代理
 /// </summary>
 /// <param name="objhttpItem">参数对象</param>
 private void SetProxy(HttpItem objhttpItem)
 {
     if (string.IsNullOrEmpty(objhttpItem.ProxyUserName) && string.IsNullOrEmpty(objhttpItem.ProxyPwd) && string.IsNullOrEmpty(objhttpItem.ProxyIp))
     {
         //不需要设置
     }
     else
     {
         //设置代理服务器
         WebProxy myProxy = new WebProxy(objhttpItem.ProxyIp, false);
         //建议连接
         myProxy.Credentials = new NetworkCredential(objhttpItem.ProxyUserName, objhttpItem.ProxyPwd);
         //给当前请求对象
         request.Proxy = myProxy;
         //设置安全凭证
         request.Credentials = CredentialCache.DefaultNetworkCredentials;
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// 设置Cookie
 /// </summary>
 /// <param name="objhttpItem">Http参数</param>
 private void SetCookie(HttpItem objhttpItem)
 {
     if (!string.IsNullOrEmpty(objhttpItem.Cookie))
     {
         //Cookie
         request.Headers[HttpRequestHeader.Cookie] = objhttpItem.Cookie;
     }
     //设置Cookie
     if (objhttpItem.CookieCollection != null)
     {
         request.CookieContainer = new CookieContainer();
         request.CookieContainer.Add(objhttpItem.CookieCollection);
     }
 }
Exemplo n.º 7
0
 /// <summary>
 /// 设置证书
 /// </summary>
 /// <param name="objhttpItem"></param>
 private void SetCer(HttpItem objhttpItem)
 {
     if (!string.IsNullOrEmpty(objhttpItem.CerPath))
     {
         //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。
         ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
         //初始化对像,并设置请求的URL地址
         request = (HttpWebRequest)WebRequest.Create(GetUrl(objhttpItem.URL));
         //创建证书文件
         X509Certificate objx509 = new X509Certificate(objhttpItem.CerPath);
         //添加到请求里
         request.ClientCertificates.Add(objx509);
     }
     else
     {
         //初始化对像,并设置请求的URL地址
         request = (HttpWebRequest)WebRequest.Create(GetUrl(objhttpItem.URL));
     }
 }
Exemplo n.º 8
0
        /// <summary>
        /// 根据相传入的数据,得到相应页面数据
        /// </summary>
        /// <param name="strPostdata">传入的数据Post方式,get方式传NUll或者空字符串都可以</param>
        /// <returns>string类型的响应数据</returns>
        private HttpResult GetHttpRequestData(HttpItem objhttpitem)
        {
            //返回参数
            HttpResult result = new HttpResult();
            try
            {
                #region 得到请求的response
                using (response = (HttpWebResponse)request.GetResponse())
                {
                    result.Header = response.Headers;
                    if (response.Cookies != null)
                    {
                        result.CookieCollection = response.Cookies;
                    }
                    if (response.Headers["set-cookie"] != null)
                    {
                        result.Cookie = response.Headers["set-cookie"];
                    }

                    MemoryStream _stream = new MemoryStream();
                    //GZIIP处理
                    if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                    {
                        //开始读取流并设置编码方式
                        //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240);
                        //.net4.0以下写法
                        _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
                    }
                    else
                    {
                        //开始读取流并设置编码方式
                        //response.GetResponseStream().CopyTo(_stream, 10240);
                        //.net4.0以下写法
                        _stream = GetMemoryStream(response.GetResponseStream());
                    }
                    //获取Byte
                    byte[] RawResponse = _stream.ToArray();
                    //是否返回Byte类型数据
                    if (objhttpitem.ResultType == ResultType.Byte)
                    {
                        result.ResultByte = RawResponse;
                    }
                    //从这里开始我们要无视编码了
                    if (encoding == null)
                    {
                        string temp = Encoding.Default.GetString(RawResponse, 0, RawResponse.Length);
                        //<meta(.*?)charset([\s]?)=[^>](.*?)>
                        Match meta = Regex.Match(temp, "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                        string charter = (meta.Groups.Count > 2) ? meta.Groups[2].Value : string.Empty;
                        charter = charter.Replace("\"", string.Empty).Replace("'", string.Empty).Replace(";", string.Empty);
                        if (charter.Length > 0)
                        {
                            charter = charter.ToLower().Replace("iso-8859-1", "gbk");
                            encoding = Encoding.GetEncoding(charter);
                        }
                        else
                        {
                            if (response.CharacterSet.ToLower().Trim() == "iso-8859-1")
                            {
                                encoding = Encoding.GetEncoding("gbk");
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(response.CharacterSet.Trim()))
                                {
                                    encoding = Encoding.UTF8;
                                }
                                else
                                {
                                    encoding = Encoding.GetEncoding(response.CharacterSet);
                                }
                            }
                        }
                    }
                    //得到返回的HTML
                    result.Html = encoding.GetString(RawResponse);
                    //最后释放流
                    _stream.Close();
                }
                #endregion
            }
            catch (WebException ex)
            {
                //这里是在发生异常时返回的错误信息
                result.Html = "String Error";
                response = (HttpWebResponse)ex.Response;
            }
            if (objhttpitem.IsToLower)
            {
                result.Html = result.Html.ToLower();
            }
            return result;
        }
Exemplo n.º 9
0
 /// <summary>
 /// 根据相传入的数据,得到相应页面数据
 /// </summary>
 /// <param name="item">参数类对象</param>
 /// <returns>返回HttpResult类型</returns>
 public HttpResult GetHtml(HttpItem item)
 {
     //返回参数
     HttpResult result = new HttpResult();
     try
     {
         //准备参数
         SetRequest(item);
     }
     catch (Exception ex)
     {
         result = new HttpResult();
         result.Cookie = string.Empty;
         result.Header = null;
         result.Html = ex.Message;
         result.StatusDescription = "配置参数时出错:" + ex.Message;
         return result;
     }
     try
     {
         #region 得到请求的response
         using (response = (HttpWebResponse)request.GetResponse())
         {
             result.StatusCode = response.StatusCode;
             result.StatusDescription = response.StatusDescription;
             result.Header = response.Headers;
             if (response.Cookies != null) result.CookieCollection = response.Cookies;
             if (response.Headers["set-cookie"] != null) result.Cookie = response.Headers["set-cookie"];
             MemoryStream _stream = new MemoryStream();
             //GZIIP处理
             if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
             {
                 //开始读取流并设置编码方式
                 //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240);
                 //.net4.0以下写法
                 _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
             }
             else
             {
                 //开始读取流并设置编码方式
                 //response.GetResponseStream().CopyTo(_stream, 10240);
                 //.net4.0以下写法
                 _stream = GetMemoryStream(response.GetResponseStream());
             }
             //获取Byte
             byte[] ResponseByte = _stream.ToArray();
             _stream.Close();
             if (ResponseByte != null & ResponseByte.Length > 0)
             {
                 //是否返回Byte类型数据
                 if (item.ResultType == ResultType.Byte) result.ResultByte = ResponseByte;
                 //从这里开始我们要无视编码了
                 if (encoding == null)
                 {
                     Match meta = Regex.Match(Encoding.Default.GetString(ResponseByte), "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase);
                     string c = (meta.Groups.Count > 1) ? meta.Groups[2].Value.ToLower().Trim() : string.Empty;
                     if (c.Length > 2)
                     {
                         try
                         {
                             if (c.IndexOf(" ") > 0) c = c.Substring(0, c.IndexOf(" "));
                             encoding = Encoding.GetEncoding(c.Replace("\"", "").Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk").Trim());
                         }
                         catch
                         {
                             if (string.IsNullOrEmpty(response.CharacterSet)) encoding = Encoding.UTF8;
                             else encoding = Encoding.GetEncoding(response.CharacterSet);
                         }
                     }
                     else
                     {
                         if (string.IsNullOrEmpty(response.CharacterSet)) encoding = Encoding.UTF8;
                         else encoding = Encoding.GetEncoding(response.CharacterSet);
                     }
                 }
                 //得到返回的HTML
                 result.Html = encoding.GetString(ResponseByte);
             }
             else
             {
                 //得到返回的HTML
                 result.Html = "本次请求并未返回任何数据";
             }
         }
         #endregion
     }
     catch (WebException ex)
     {
         //这里是在发生异常时返回的错误信息
         response = (HttpWebResponse)ex.Response;
         result.Html = ex.Message;
         if (response != null)
         {
             result.StatusCode = response.StatusCode;
             result.StatusDescription = response.StatusDescription;
         }
     }
     catch (Exception ex)
     {
         result.Html = ex.Message;
     }
     if (item.IsToLower) result.Html = result.Html.ToLower();
     return result;
 }
Exemplo n.º 10
0
 /// <summary>
 /// 为请求准备参数
 /// </summary>
 ///<param name="objhttpItem">参数列表</param>
 private void SetRequest(HttpItem objhttpItem)
 {
     //设置安全协议
     ServicePointManager.SecurityProtocol = objhttpItem.SecurityProtocolType;
     // 验证证书
     SetCer(objhttpItem);
     //设置Header参数
     if (objhttpItem.Header != null && objhttpItem.Header.Count > 0) foreach (string item in objhttpItem.Header.AllKeys)
         {
             request.Headers.Add(item, objhttpItem.Header[item]);
         }
     // 设置代理
     SetProxy(objhttpItem);
     if (objhttpItem.ProtocolVersion != null) request.ProtocolVersion = objhttpItem.ProtocolVersion;
     request.ServicePoint.Expect100Continue = objhttpItem.Expect100Continue;
     //请求方式Get或者Post
     request.Method = objhttpItem.Method;
     request.Timeout = objhttpItem.Timeout;
     request.ReadWriteTimeout = objhttpItem.ReadWriteTimeout;
     //Accept
     request.Accept = objhttpItem.Accept;
     //ContentType返回类型
     request.ContentType = objhttpItem.ContentType;
     //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息
     request.UserAgent = objhttpItem.UserAgent;
     // 编码
     encoding = objhttpItem.Encoding;
     //设置Cookie
     SetCookie(objhttpItem);
     //来源地址
     request.Referer = objhttpItem.Referer;
     //是否执行跳转功能
     request.AllowAutoRedirect = objhttpItem.Allowautoredirect;
     //设置Post数据
     SetPostData(objhttpItem);
     //设置最大连接
     if (objhttpItem.Connectionlimit > 0) request.ServicePoint.ConnectionLimit = objhttpItem.Connectionlimit;
 }
Exemplo n.º 11
0
 /// <summary>
 /// 设置多个证书
 /// </summary>
 /// <param name="objhttpItem"></param>
 private void SetCerList(HttpItem objhttpItem)
 {
     if (objhttpItem.ClentCertificates != null && objhttpItem.ClentCertificates.Count > 0)
     {
         foreach (X509Certificate item in objhttpItem.ClentCertificates)
         {
             request.ClientCertificates.Add(item);
         }
     }
 }
Exemplo n.º 12
0
        /// <summary>
        /// 根据相传入的数据,得到相应页面数据
        /// </summary>
        /// <param name="strPostdata">传入的数据Post方式,get方式传NUll或者空字符串都可以</param>
        /// <returns>string类型的响应数据</returns>
        private HttpResult GetHttpRequestData(HttpItem objhttpitem)
        {
            //返回参数
            HttpResult result = new HttpResult();

            try
            {
                #region 得到请求的response
                using (response = (HttpWebResponse)request.GetResponse())
                {
                    result.Header = response.Headers;
                    if (response.Cookies != null)
                    {
                        result.CookieCollection = response.Cookies;
                    }
                    if (response.Headers["set-cookie"] != null)
                    {
                        result.Cookie = response.Headers["set-cookie"];
                    }

                    MemoryStream _stream = new MemoryStream();
                    //GZIIP处理
                    if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                    {
                        //开始读取流并设置编码方式
                        //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240);
                        //.net4.0以下写法
                        _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
                    }
                    else
                    {
                        //开始读取流并设置编码方式
                        //response.GetResponseStream().CopyTo(_stream, 10240);
                        //.net4.0以下写法
                        _stream = GetMemoryStream(response.GetResponseStream());
                    }
                    //获取Byte
                    byte[] RawResponse = _stream.ToArray();
                    //是否返回Byte类型数据
                    if (objhttpitem.ResultType == ResultType.Byte)
                    {
                        result.ResultByte = RawResponse;
                    }
                    //从这里开始我们要无视编码了
                    if (encoding == null)
                    {
                        string temp = Encoding.Default.GetString(RawResponse, 0, RawResponse.Length);
                        //<meta(.*?)charset([\s]?)=[^>](.*?)>
                        Match  meta    = Regex.Match(temp, "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                        string charter = (meta.Groups.Count > 2) ? meta.Groups[2].Value : string.Empty;
                        charter = charter.Replace("\"", string.Empty).Replace("'", string.Empty).Replace(";", string.Empty);
                        if (charter.Length > 0)
                        {
                            charter  = charter.ToLower().Replace("iso-8859-1", "gbk");
                            encoding = Encoding.GetEncoding(charter);
                        }
                        else
                        {
                            if (response.CharacterSet.ToLower().Trim() == "iso-8859-1")
                            {
                                encoding = Encoding.GetEncoding("gbk");
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(response.CharacterSet.Trim()))
                                {
                                    encoding = Encoding.UTF8;
                                }
                                else
                                {
                                    encoding = Encoding.GetEncoding(response.CharacterSet);
                                }
                            }
                        }
                    }
                    //得到返回的HTML
                    result.Html = encoding.GetString(RawResponse);
                    //最后释放流
                    _stream.Close();
                }
                #endregion
            }
            catch (WebException ex)
            {
                //这里是在发生异常时返回的错误信息
                result.Html = "String Error";
                response    = (HttpWebResponse)ex.Response;
            }
            if (objhttpitem.IsToLower)
            {
                result.Html = result.Html.ToLower();
            }
            return(result);
        }
Exemplo n.º 13
0
 /// <summary>
 /// 为请求准备参数
 /// </summary>
 ///<param name="item">参数列表</param>
 private void SetRequest(HttpItem item)
 {
     // 验证证书
     SetCer(item);
     if (item.IPEndPoint != null)
     {
         _IPEndPoint = item.IPEndPoint;
         //设置本地的出口ip和端口
         request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);
     }
     //设置Header参数
     if (item.Header != null && item.Header.Count > 0)
     {
         foreach (string key in item.Header.AllKeys)
         {
             request.Headers.Add(key, item.Header[key]);
         }
     }
     // 设置代理
     SetProxy(item);
     if (item.ProtocolVersion != null)
     {
         request.ProtocolVersion = item.ProtocolVersion;
     }
     request.ServicePoint.Expect100Continue = item.Expect100Continue;
     //请求方式Get或者Post
     request.Method           = item.Method;
     request.Timeout          = item.Timeout;
     request.KeepAlive        = item.KeepAlive;
     request.ReadWriteTimeout = item.ReadWriteTimeout;
     if (item.IfModifiedSince != null)
     {
         request.IfModifiedSince = Convert.ToDateTime(item.IfModifiedSince);
     }
     //Accept
     request.Accept = item.Accept;
     //ContentType返回类型
     request.ContentType = item.ContentType;
     //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息
     request.UserAgent = item.UserAgent;
     // 编码
     encoding = item.Encoding;
     //设置安全凭证
     request.Credentials = item.ICredentials;
     //设置Cookie
     SetCookie(item);
     //来源地址
     request.Referer = item.Referer;
     //是否执行跳转功能
     request.AllowAutoRedirect = item.Allowautoredirect;
     if (item.MaximumAutomaticRedirections > 0)
     {
         request.MaximumAutomaticRedirections = item.MaximumAutomaticRedirections;
     }
     //设置Post数据
     SetPostData(item);
     //设置最大连接
     if (item.Connectionlimit > 0)
     {
         request.ServicePoint.ConnectionLimit = item.Connectionlimit;
     }
 }
Exemplo n.º 14
0
        /// <summary>
        /// 根据相传入的数据,得到相应页面数据
        /// </summary>
        /// <param name="objhttpitem">参数类对象</param>
        /// <returns>返回HttpResult类型</returns>
        public HttpResult GetHtml(HttpItem objhttpitem)
        {
            //返回参数
            HttpResult result = new HttpResult();

            try
            {
                //准备参数
                SetRequest(objhttpitem);
            }
            catch (Exception ex)
            {
                result = new HttpResult()
                {
                    Cookie            = "",
                    Header            = null,
                    Html              = ex.Message,
                    StatusDescription = "配置参数时出错:" + ex.Message
                };
                return(result);
            }
            try
            {
                #region 得到请求的response
                using (response = (HttpWebResponse)request.GetResponse())
                {
                    result.StatusCode        = response.StatusCode;
                    result.StatusDescription = response.StatusDescription;
                    result.Header            = response.Headers;
                    if (response.Cookies != null)
                    {
                        result.CookieCollection = response.Cookies;
                    }
                    if (response.Headers["set-cookie"] != null)
                    {
                        result.Cookie = response.Headers["set-cookie"];
                    }
                    MemoryStream _stream = new MemoryStream();
                    //GZIIP处理
                    if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                    {
                        //开始读取流并设置编码方式
                        //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240);
                        //.net4.0以下写法
                        _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
                    }
                    else
                    {
                        //开始读取流并设置编码方式
                        //response.GetResponseStream().CopyTo(_stream, 10240);
                        //.net4.0以下写法
                        _stream = GetMemoryStream(response.GetResponseStream());
                    }
                    //获取Byte
                    byte[] RawResponse = _stream.ToArray();
                    _stream.Close();
                    //是否返回Byte类型数据
                    if (objhttpitem.ResultType == ResultType.Byte)
                    {
                        result.ResultByte = RawResponse;
                    }
                    //从这里开始我们要无视编码了
                    if (encoding == null)
                    {
                        Match  meta    = Regex.Match(Encoding.Default.GetString(RawResponse), "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase);
                        string charter = (meta.Groups.Count > 1) ? meta.Groups[2].Value.ToLower() : string.Empty;
                        if (charter.Length > 2)
                        {
                            encoding = Encoding.GetEncoding(charter.Trim().Replace("\"", "").Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk"));
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(response.CharacterSet))
                            {
                                encoding = Encoding.UTF8;
                            }
                            else
                            {
                                encoding = Encoding.GetEncoding(response.CharacterSet);
                            }
                        }
                    }
                    //得到返回的HTML
                    result.Html = encoding.GetString(RawResponse);
                }
                #endregion
            }
            catch (WebException ex)
            {
                //这里是在发生异常时返回的错误信息
                response    = (HttpWebResponse)ex.Response;
                result.Html = ex.Message;
                if (response != null)
                {
                    result.StatusCode        = response.StatusCode;
                    result.StatusDescription = response.StatusDescription;
                }
            }
            catch (Exception ex)
            {
                result.Html = ex.Message;
            }
            if (objhttpitem.IsToLower)
            {
                result.Html = result.Html.ToLower();
            }
            return(result);
        }
Exemplo n.º 15
0
 /// <summary>
 /// 设置代理
 /// </summary>
 /// <param name="item">参数对象</param>
 private void SetProxy(HttpItem item)
 {
     bool isIeProxy = false;
     if (!string.IsNullOrEmpty(item.ProxyIp))
     {
         isIeProxy = item.ProxyIp.ToLower().Contains("ieproxy");
     }
     if (!string.IsNullOrEmpty(item.ProxyIp) && !isIeProxy)
     {
         //设置代理服务器
         if (item.ProxyIp.Contains(":"))
         {
             string[] plist = item.ProxyIp.Split(':');
             WebProxy myProxy = new WebProxy(plist[0].Trim(), Convert.ToInt32(plist[1].Trim()));
             //建议连接
             myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd);
             //给当前请求对象
             request.Proxy = myProxy;
         }
         else
         {
             WebProxy myProxy = new WebProxy(item.ProxyIp, false);
             //建议连接
             myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd);
             //给当前请求对象
             request.Proxy = myProxy;
         }
     }
     else if (isIeProxy)
     {
         //设置为IE代理
     }
     else
     {
         request.Proxy = item.WebProxy;
     }
 }
Exemplo n.º 16
0
 /// <summary>
 /// 设置代理
 /// </summary>
 /// <param name="item">参数对象</param>
 private void SetProxy(HttpItem item)
 {
     if (!string.IsNullOrEmpty(item.ProxyIp))
     {
         //设置代理服务器
         if (item.ProxyIp.Contains(":"))
         {
             string[] plist = item.ProxyIp.Split(':');
             WebProxy myProxy = new WebProxy(plist[0].Trim(), Convert.ToInt32(plist[1].Trim()));
             //建议连接
             myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd);
             //给当前请求对象
             request.Proxy = myProxy;
         }
         else
         {
             WebProxy myProxy = new WebProxy(item.ProxyIp, false);
             //建议连接
             myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd);
             //给当前请求对象
             request.Proxy = myProxy;
         }
         //设置安全凭证
         request.Credentials = CredentialCache.DefaultCredentials;
     }
     else if (item.WebProxy != null)
     {
         request.Proxy = item.WebProxy;
     }
 }
Exemplo n.º 17
0
 /// <summary>
 /// 为请求准备参数
 /// </summary>
 ///<param name="item">参数列表</param>
 private void SetRequest(HttpItem item)
 {
     // 验证证书
     SetCer(item);
     if (item.IPEndPoint != null)
     {
         _IPEndPoint = item.IPEndPoint;
         //设置本地的出口ip和端口
         request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);
     }
     //设置Header参数
     if (item.Header != null && item.Header.Count > 0) foreach (string key in item.Header.AllKeys)
         {
             request.Headers.Add(key, item.Header[key]);
         }
     // 设置代理
     SetProxy(item);
     if (item.ProtocolVersion != null) request.ProtocolVersion = item.ProtocolVersion;
     request.ServicePoint.Expect100Continue = item.Expect100Continue;
     //请求方式Get或者Post
     request.Method = item.Method;
     request.Timeout = item.Timeout;
     request.KeepAlive = item.KeepAlive;
     request.ReadWriteTimeout = item.ReadWriteTimeout;
     if (item.IfModifiedSince != null) request.IfModifiedSince = Convert.ToDateTime(item.IfModifiedSince);
     //Accept
     request.Accept = item.Accept;
     //ContentType返回类型
     request.ContentType = item.ContentType;
     //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息
     request.UserAgent = item.UserAgent;
     // 编码
     encoding = item.Encoding;
     //设置安全凭证
     request.Credentials = item.ICredentials;
     //设置Cookie
     SetCookie(item);
     //来源地址
     request.Referer = item.Referer;
     //是否执行跳转功能
     request.AllowAutoRedirect = item.Allowautoredirect;
     if (item.MaximumAutomaticRedirections > 0)
     {
         request.MaximumAutomaticRedirections = item.MaximumAutomaticRedirections;
     }
     //设置Post数据
     SetPostData(item);
     //设置最大连接
     if (item.Connectionlimit > 0) request.ServicePoint.ConnectionLimit = item.Connectionlimit;
 }
Exemplo n.º 18
0
 /// <summary>
 /// 为请求准备参数
 /// </summary>
 ///<param name="item">参数列表</param>
 private void SetRequest(HttpItem item)
 {
     // 验证证书
     SetCer(item);
     //设置Header参数
     if (item.Header != null && item.Header.Count > 0) foreach (string key in item.Header.AllKeys)
         {
             request.Headers.Add(key, item.Header[key]);
         }
     // 设置代理
     SetProxy(item);
     if (item.ProtocolVersion != null) request.ProtocolVersion = item.ProtocolVersion;
     request.ServicePoint.Expect100Continue = item.Expect100Continue;
     //请求方式Get或者Post
     request.Method = item.Method;
     request.Timeout = item.Timeout;
     request.KeepAlive = item.KeepAlive;
     request.ReadWriteTimeout = item.ReadWriteTimeout;
     //Accept
     request.Accept = item.Accept;
     //ContentType返回类型
     request.ContentType = item.ContentType;
     //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息
     request.UserAgent = item.UserAgent;
     // 编码
     encoding = item.Encoding;
     //设置Cookie
     SetCookie(item);
     //来源地址
     request.Referer = item.Referer;
     //是否执行跳转功能
     request.AllowAutoRedirect = item.Allowautoredirect;
     //设置Post数据
     SetPostData(item);
     //设置最大连接
     if (item.Connectionlimit > 0) request.ServicePoint.ConnectionLimit = item.Connectionlimit;
 }
Exemplo n.º 19
0
 /// <summary>
 /// 为请求准备参数
 /// </summary>
 ///<param name="objhttpItem">参数列表</param>
 /// <param name="_Encoding">读取数据时的编码方式</param>
 private void SetRequest(HttpItem objhttpItem)
 {
     // 验证证书
     SetCer(objhttpItem);
     // 设置代理
     SetProxy(objhttpItem);
     //请求方式Get或者Post
     request.Method = objhttpItem.Method;
     request.Timeout = objhttpItem.Timeout;
     request.ReadWriteTimeout = objhttpItem.ReadWriteTimeout;
     //Accept
     request.Accept = objhttpItem.Accept;
     //ContentType返回类型
     request.ContentType = objhttpItem.ContentType;
     //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息
     request.UserAgent = objhttpItem.UserAgent;
     // 编码
     SetEncoding(objhttpItem);
     //设置Cookie
     SetCookie(objhttpItem);
     //来源地址
     request.Referer = objhttpItem.Referer;
     //是否执行跳转功能
     request.AllowAutoRedirect = objhttpItem.Allowautoredirect;
     //设置Post数据
     SetPostData(objhttpItem);
     //设置最大连接
     if (objhttpItem.Connectionlimit > 0)
     {
         request.ServicePoint.ConnectionLimit = objhttpItem.Connectionlimit;
     }
 }
Exemplo n.º 20
0
        /// <summary>
        /// 获取数据的并解析的方法
        /// </summary>
        /// <param name="item"></param>
        /// <param name="result"></param>
        private void GetData(HttpItem item, HttpResult result)
        {
            #region base
            //获取StatusCode
            result.StatusCode = response.StatusCode;
            //获取StatusDescription
            result.StatusDescription = response.StatusDescription;
            //获取最后访问的URl
            result.ResponseUri = response.ResponseUri.ToString();
            //获取Headers
            result.Header = response.Headers;
            //获取CookieCollection
            if (response.Cookies != null) result.CookieCollection = response.Cookies;
            //获取set-cookie
            if (response.Headers["set-cookie"] != null) result.Cookie = response.Headers["set-cookie"];
            #endregion

            #region byte
            //处理网页Byte
            byte[] ResponseByte = GetByte();
            #endregion

            #region Html
            if (ResponseByte != null & ResponseByte.Length > 0)
            {
                //设置编码
                SetEncoding(item, result, ResponseByte);
                //得到返回的HTML
                result.Html = encoding.GetString(ResponseByte);
            }
            else
            {
                //没有返回任何Html代码
                result.Html = string.Empty;
            }
            #endregion
        }
Exemplo n.º 21
0
 /// <summary>
 /// 设置编码
 /// </summary>
 /// <param name="objhttpItem">Http参数</param>
 private void SetEncoding(HttpItem objhttpItem)
 {
     if (string.IsNullOrEmpty(objhttpItem.Encoding) || objhttpItem.Encoding.ToLower().Trim() == "null")
     {
         //读取数据时的编码方式
         encoding = null;
     }
     else
     {
         //读取数据时的编码方式
         encoding = System.Text.Encoding.GetEncoding(objhttpItem.Encoding);
     }
 }
Exemplo n.º 22
0
 /// <summary>
 /// 设置多个证书
 /// </summary>
 /// <param name="item"></param>
 private void SetCerList(HttpItem item)
 {
     if (item.ClentCertificates != null && item.ClentCertificates.Count > 0)
     {
         foreach (X509Certificate c in item.ClentCertificates)
         {
             request.ClientCertificates.Add(c);
         }
     }
 }
Exemplo n.º 23
0
 /// <summary>
 /// 设置Post数据
 /// </summary>
 /// <param name="objhttpItem">Http参数</param>
 private void SetPostData(HttpItem objhttpItem)
 {
     //验证在得到结果时是否有传入数据
     if (request.Method.Trim().ToLower().Contains("post"))
     {
         //写入Byte类型
         if (objhttpItem.PostDataType == PostDataType.Byte)
         {
             //验证在得到结果时是否有传入数据
             if (objhttpItem.PostdataByte != null && objhttpItem.PostdataByte.Length > 0)
             {
                 request.ContentLength = objhttpItem.PostdataByte.Length;
                 request.GetRequestStream().Write(objhttpItem.PostdataByte, 0, objhttpItem.PostdataByte.Length);
             }
         }//写入文件
         else if (objhttpItem.PostDataType == PostDataType.FilePath)
         {
             StreamReader r = new StreamReader(objhttpItem.Postdata, encoding);
             byte[] buffer = Encoding.Default.GetBytes(r.ReadToEnd());
             r.Close();
             request.ContentLength = buffer.Length;
             request.GetRequestStream().Write(buffer, 0, buffer.Length);
         }
         else
         {
             //验证在得到结果时是否有传入数据
             if (!string.IsNullOrEmpty(objhttpItem.Postdata))
             {
                 byte[] buffer = Encoding.Default.GetBytes(objhttpItem.Postdata);
                 request.ContentLength = buffer.Length;
                 request.GetRequestStream().Write(buffer, 0, buffer.Length);
             }
         }
     }
 }
Exemplo n.º 24
0
 /// <summary>
 /// 设置Cookie
 /// </summary>
 /// <param name="item">Http参数</param>
 private void SetCookie(HttpItem item)
 {
     if (!string.IsNullOrEmpty(item.Cookie)) request.Headers[HttpRequestHeader.Cookie] = item.Cookie;
     //设置CookieCollection
     if (item.ResultCookieType == ResultCookieType.CookieCollection)
     {
         request.CookieContainer = new CookieContainer();
         if (item.CookieCollection != null && item.CookieCollection.Count > 0)
             request.CookieContainer.Add(item.CookieCollection);
     }
 }
Exemplo n.º 25
0
 ///<summary>
 ///采用https协议访问网络,根据传入的URl地址,得到响应的数据字符串。
 ///</summary>
 ///<param name="objhttpItem">参数列表</param>
 ///<returns>String类型的数据</returns>
 public HttpResult GetHtml(HttpItem objhttpItem)
 {
     //准备参数
     SetRequest(objhttpItem);
     //调用专门读取数据的类
     return GetHttpRequestData(objhttpItem);
 }
Exemplo n.º 26
0
        static void Main(string[] args)
        {
            //List<Thread> threads = new List<Thread>();
            while (true)
            {
                Console.WriteLine("请输入关键词:");
                String keyWord = Console.ReadLine().Trim();
                if (!string.IsNullOrEmpty(keyWord.Trim()))
                {
                    //           
                    Thread t = new Thread(() =>
                    {
                        #region 抓取任务开始
                        String start = "";
                        string taskKeyWord = keyWord;
                        HttpHelper http = new HttpHelper();
                        //每一次抓取
                        //大于totalItems/48时停止
                        Int32 count = 1;
                        Random random = new Random();
                        while (true)
                        {
                            start = (48 * count).ToString();
                            HttpItem item = new HttpItem()
                            {
                                URL = "http://pic.sogou.com/pics?query=" + taskKeyWord + "&mood=0&picformat=0&mode=1&di=0&clusterfilter=off&p=40230504&dp=1&start=" + start + "&reqType=ajax&tn=0&reqFrom=result",//URL     必需项    
                                Method = "get",//URL     可选项 默认为Get   
                                IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写                                   
                                Referer = "",//来源URL     可选项   
                                Postdata = "",//Post数据     可选项GET时不需要写   
                                Timeout = 100000,//连接超时时间     可选项默认为100000    
                                ReadWriteTimeout = 30000,//写入Post数据超时时间     可选项默认为30000   
                                UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统     可选项有默认值   
                                ContentType = "text/html",//返回类型    可选项有默认值   
                                Allowautoredirect = false,//是否根据301跳转     可选项   
                                                          //CerPath = "d:\123.cer",//证书绝对路径     可选项不需要证书时可以不写这个参数   
                                                          //Connectionlimit = 1024,//最大连接数     可选项 默认为1024                            
                                ResultType = ResultType.String
                            };
                            HttpResult result = http.GetHtml(item);
                            string html = result.Html;
                            string cookie = result.Cookie;
                            //Console.WriteLine("【" + Thread.CurrentThread.ManagedThreadId + "】-" + html);
                            //解析结果,得到停止条件                            
                            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
                            SouGouImgModel model = js.Deserialize<SouGouImgModel>(html);

                            if (Convert.ToInt32(start) >= Convert.ToInt32(model.totalItems.Replace(",", "")))
                            //if (model.items.Count==0)
                            {
                                Console.WriteLine("结束时间:【" + DateTime.Now + "】-任务关键字:" + taskKeyWord);
                                break;
                            }
                           
                            Console.WriteLine("当前个数:"+model.items.Count+"==请求总个数:"+start+"==MaxEnd:"+model.maxEnd+"==总个数:"+model.totalItems);
                            //下一轮                
                            int span = random.Next(3000, 10000);
                            Thread.Sleep(span);
                            //Console.WriteLine(count);
                            count++;

                        }
                        #endregion

                    });
                    //threads.Add(t);
                    t.Start();
                    Console.WriteLine("开始时间:【" + DateTime.Now + "】-任务关键字:" + keyWord);

                }
            }

        }