Ejemplo n.º 1
0
        /// <summary>
        /// 设置Post数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="requestConfig"></param>
        private void SetPostData(HttpWebRequest request, HttpRequestConfig requestConfig)
        {
            //验证在得到结果时是否有传入数据
            if (requestConfig.Method.ToLower().Equals("post"))
            {
                byte[] buffer = null;

                //写入字符串
                if (requestConfig.PostDataType == PostDataType.String && !string.IsNullOrWhiteSpace(requestConfig.PostData))
                {
                    buffer = requestConfig.RequestEncoding.GetBytes(requestConfig.PostData);
                }
                //写入Byte类型
                else if (requestConfig.PostDataType == PostDataType.Byte && requestConfig.PostDataByte != null && requestConfig.PostDataByte.Length > 0)
                {
                    //验证在得到结果时是否有传入数据
                    buffer = requestConfig.PostDataByte;
                }
                //写入文件
                else if (requestConfig.PostDataType == PostDataType.FilePath && !string.IsNullOrWhiteSpace(requestConfig.PostData))
                {
                    using (StreamReader r = new StreamReader(requestConfig.PostData, requestConfig.RequestEncoding))
                    {
                        buffer = requestConfig.RequestEncoding.GetBytes(r.ReadToEnd());
                        r.Close();
                    }
                }

                if (buffer != null)
                {
                    request.ContentLength = buffer.Length;
                    request.GetRequestStream().Write(buffer, 0, buffer.Length);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 设置代理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="httpRequestConfig"></param>
        private void SetProxy(HttpWebRequest request, HttpRequestConfig requestConfig)
        {
            if (!string.IsNullOrWhiteSpace(requestConfig.ProxyServerIPAndPort))
            {
                //设置代理服务器
                if (requestConfig.ProxyServerIPAndPort.Contains(":"))
                {
                    string[] plist = requestConfig.ProxyServerIPAndPort.Split(':');

                    WebProxy myProxy = new WebProxy(plist[0].Trim(), Convert.ToInt32(plist[1].Trim()));

                    myProxy.Credentials = new NetworkCredential(requestConfig.ProxyServerUserName, requestConfig.ProxyServerPassword);

                    //设置代理
                    request.Proxy = myProxy;
                }
                else
                {
                    WebProxy myProxy = new WebProxy(requestConfig.ProxyServerIPAndPort, false);

                    myProxy.Credentials = new NetworkCredential(requestConfig.ProxyServerUserName, requestConfig.ProxyServerPassword);

                    //设置代理
                    request.Proxy = myProxy;
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 设置 Header
        /// </summary>
        /// <param name="request"></param>
        /// <param name="requestConfig"></param>
        private void SetHeader(HttpWebRequest request, HttpRequestConfig requestConfig)
        {
            if (requestConfig.Header != null && requestConfig.Header.Count > 0)
            {
                foreach (string key in requestConfig.Header.AllKeys)
                {
                    request.Headers.Add(key, requestConfig.Header[key]);
                }
            }

            if (!request.Headers.AllKeys.Contains("Cache-Control"))
            {
                request.Headers.Add("Cache-Control", "no-cache");
            }

            if (!request.Headers.AllKeys.Contains("UA-CPU"))
            {
                request.Headers.Add("UA-CPU", "x86");
            }

            if (!request.Headers.AllKeys.Contains("Accept-Language"))
            {
                request.Headers.Add("Accept-Language", "zh-cn");
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="request"></param>
        /// <param name="requestConfig"></param>
        private void SetCookie(HttpWebRequest request, HttpRequestConfig requestConfig)
        {
            if (!string.IsNullOrWhiteSpace(requestConfig.Cookie))
            {
                request.Headers[HttpRequestHeader.Cookie] = requestConfig.Cookie;
            }

            //设置CookieCollection
            if (requestConfig.CookieCollection != null && requestConfig.CookieCollection.Count > 0)
            {
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(requestConfig.CookieCollection);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 创建 Request
        /// </summary>
        /// <param name="requestConfig"></param>
        /// <returns></returns>
        private HttpWebRequest CreateRequest(HttpRequestConfig requestConfig)
        {
            //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);

            HttpWebRequest request = null;

            //设置证书
            if (!string.IsNullOrWhiteSpace(requestConfig.CertificatePath))
            {
                //初始化对像,并设置请求的URL地址
                request = (HttpWebRequest)WebRequest.Create(requestConfig.Url);

                //设置X509Certificate证书
                if (requestConfig.CertificateCollection != null && requestConfig.CertificateCollection.Count > 0)
                {
                    foreach (X509Certificate c in requestConfig.CertificateCollection)
                    {
                        request.ClientCertificates.Add(c);
                    }
                }

                //将证书添加到请求里
                request.ClientCertificates.Add(new X509Certificate(requestConfig.CertificatePath));
            }
            else
            {
                //初始化对像,并设置请求的URL地址
                request = (HttpWebRequest)WebRequest.Create(requestConfig.Url);

                //设置X509Certificate证书
                if (requestConfig.CertificateCollection != null && requestConfig.CertificateCollection.Count > 0)
                {
                    foreach (X509Certificate c in requestConfig.CertificateCollection)
                    {
                        request.ClientCertificates.Add(c);
                    }
                }
            }

            request.ServicePoint.Expect100Continue = false;
            request.Method = requestConfig.Method;//请求方式Get或者Post
            request.Timeout = 100000;//请求超时时间
            request.KeepAlive = true;//长连接
            request.Accept = requestConfig.Accept;//Accept
            request.ContentType = requestConfig.ContentType;//ContentType返回类型
            request.UserAgent = requestConfig.UserAgent;//UserAgent客户端的访问类型,包括浏览器版本和操作系统信息
            request.Credentials = CredentialCache.DefaultCredentials;//设置请求的身份验证信息
            request.Referer = requestConfig.Referer;//来源地址

            return request;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Http请求
        /// </summary>
        /// <param name="requestConfig"></param>
        /// <returns></returns>
        public HttpResult GetHtml(HttpRequestConfig requestConfig)
        {
            //创建Request
            HttpWebRequest request = CreateRequest(requestConfig);

            //设置Header
            SetHeader(request, requestConfig);

            //设置代理
            SetProxy(request, requestConfig);

            //设置Cookie
            SetCookie(request, requestConfig);

            //设置Post请求数据
            SetPostData(request, requestConfig);

            HttpResult result = new HttpResult();

            using (WebResponse response = request.GetResponse())
            {
                HttpWebResponse httpResponse = response as HttpWebResponse;

                if (httpResponse != null)
                {
                    result.Headers = httpResponse.Headers;

                    result.StatusCode = httpResponse.StatusCode;

                    result.StatusDescription = httpResponse.StatusDescription;

                    if (httpResponse.Cookies != null)
                    {
                        result.CookieCollection = httpResponse.Cookies;
                    }

                    if (httpResponse.Headers["set-cookie"] != null)
                    {
                        result.Cookie = httpResponse.Headers["set-cookie"];
                    }
                }

                //返回的流
                using (Stream responseStream = response.GetResponseStream())
                {
                    //将返回流转成Byte[]
                    if (responseStream != null)
                    {
                        using (MemoryStream _stream = new MemoryStream())
                        {
                            //开始读取流并设置编码方式
                            responseStream.CopyTo(_stream);

                            //获取Byte
                            result.ResultByte = _stream.ToArray();
                        }
                    }
                    else
                    {
                        result.ResultByte = null;
                    }
                }
            }

            //将转换的Byte[]转成String
            if (requestConfig.ResultType == ResultType.String && result.ResultByte != null)
            {
                Encoding encoding = requestConfig.ResponseEncoding;

                //从这里开始我们要无视编码了
                if (encoding == null)
                {
                    Match meta = Regex.Match(Encoding.Default.GetString(result.ResultByte), "<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
                        {
                            encoding = Encoding.Default;
                        }
                    }
                    else
                    {
                        encoding = Encoding.Default;
                    }
                }
                //得到返回的HTML
                result.ResultString = encoding.GetString(result.ResultByte);
            }
            else
            {
                //得到返回的HTML
                result.ResultString = "";
            }

            return result;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Http请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="method"></param>
        /// <param name="cookieCollection"></param>
        /// <param name="referer"></param>
        /// <returns></returns>
        public HttpResult GetHtml(string url, string method = "GET", CookieCollection cookieCollection = null, string referer = "")
        {
            HttpRequestConfig requestConfig = new HttpRequestConfig();

            requestConfig.Url = url;

            requestConfig.Method = method;

            requestConfig.CookieCollection = cookieCollection;

            requestConfig.Referer = referer;

            HttpResult result = GetHtml(requestConfig);

            return result;
        }