Esempio n. 1
0
        /// <summary>
        /// Get方式获取页面
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string GetHtml(RequestModel model)
        {
            try
            {
                HttpWebRequest request = HttpWebRequest.Create(model.Url) as HttpWebRequest;
                request.MaximumAutomaticRedirections = 100;
                request.Referer           = model.Referer;
                request.Timeout           = 300000;
                request.AllowAutoRedirect = true;
                request.ContentType       = model.ContentType;
                request.UserAgent         = model.UserAgent;
                request.KeepAlive         = true;
                if (model.Url.IndexOf("https") == 0)
                {
                    ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
                }
                if (model.RequestHearder != null)
                {
                    foreach (var item in model.RequestHearder)
                    {
                        request.Headers.Add(item.Key, item.Value);
                    }
                }

                if (model.ProxyObject != null)
                {
                    request.Proxy = model.ProxyObject;
                }
                System.Net.ServicePointManager.DefaultConnectionLimit = 50;
                if (model.Host != null)
                {
                    request.Host = model.Host;
                }
                request.CookieContainer = GetCookieContainer("http://" + request.RequestUri.Authority, model.Cookie);
                request.Accept          = model.Accept;
                request.Method          = model.Method.ToString();
                request.ServicePoint.Expect100Continue = false;

                //  WriteToRequestStream(model.Data, request.GetRequestStream());
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                model.Cookie = GetCookieString(response.Cookies);
                var    stream  = response.GetResponseStream();
                string content = string.Empty;
                if (response.ContentEncoding.Contains("gzip"))
                {
                    using (GZipStream gzStream = new GZipStream(stream, CompressionMode.Decompress))
                    {
                        using (StreamReader reader = new StreamReader(gzStream, model.Encode))
                        {
                            content = reader.ReadToEnd();
                        }
                    }
                }
                else if (response.ContentEncoding.ToLower().Contains("deflate"))
                {
                    using (DeflateStream dfs = new DeflateStream(stream, CompressionMode.Decompress))
                    {
                        using (StreamReader reader = new StreamReader(dfs, model.Encode))
                        {
                            content = reader.ReadToEnd();
                        }
                    }
                }
                else
                {
                    content = StreamUtility.GetString(stream, model.Encode);
                }


                request.Abort();
                response.Close();
                return(content);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Post方式获取页面
        /// </summary>
        /// <param name="model">请求模型</param>
        /// <returns></returns>
        public static string PostHtmlCheck(RequestModel model)
        {
            try
            {
                HttpWebRequest request = HttpWebRequest.Create(model.Url) as HttpWebRequest;
                request.UserAgent   = model.UserAgent;
                request.Referer     = model.Url;
                request.ContentType = model.ContentType;

                request.Accept    = model.Accept;
                request.KeepAlive = true;
                request.ServicePoint.Expect100Continue = false;
                request.CookieContainer = GetCookieContainer(request.Referer, model.Cookie);

                if (model.RequestHearder.Count > 0)
                {
                    foreach (var item in model.RequestHearder)
                    {
                        request.Headers.Add(item.Key, item.Value);
                    }
                }

                if (model.ProxyObject != null)
                {
                    request.Proxy = model.ProxyObject;
                }

                if (model.TimeOut > 0)
                {
                    request.Timeout          = model.TimeOut * 1000;
                    request.ReadWriteTimeout = model.TimeOut * 1000;
                }
                else
                {
                    request.Timeout          = 2000;
                    request.ReadWriteTimeout = 2000;
                }

                request.Method = model.Method.ToString();
                WriteToRequestStream(model.Data, request.GetRequestStream());
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                string          content  = string.Empty;
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    var stream = response.GetResponseStream();
                    if (response.ContentEncoding.Contains("gzip"))
                    {
                        using (GZipStream gzStream = new GZipStream(stream, CompressionMode.Decompress))
                        {
                            using (StreamReader reader = new StreamReader(gzStream, model.Encode))
                            {
                                content = reader.ReadToEnd();
                            }
                        }
                    }
                    else if (response.ContentEncoding.ToLower().Contains("deflate"))
                    {
                        using (DeflateStream dfs = new DeflateStream(stream, CompressionMode.Decompress))
                        {
                            using (StreamReader reader = new StreamReader(dfs, model.Encode))
                            {
                                content = reader.ReadToEnd();
                            }
                        }
                    }
                    else
                    {
                        content = StreamUtility.GetString(stream, model.Encode);
                    }

                    request.Abort();
                    response.Close();
                    model.Cookie = GetCookieString(response.Cookies);
                }

                return(content);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }