Esempio n. 1
0
        /// <summary>
        /// 采集HTML
        /// </summary>
        /// <param name="url">采集地址</param>
        /// <param name="param">采集参数</param>
        /// <param name="method">POST,GET</param>
        /// <param name="sendencoding">发送数据的编码</param>
        /// <param name="receivencoding">接受数据的编码</param>
        /// <param name="cookie">CookieContainer</param>
        /// <returns>返回采集的数据</returns>
        public string CollectHtml(string url, string param, Method method, HtmlEncoding sendEncoding, HtmlEncoding receivEncoding)
        {
            string          html      = string.Empty;
            HttpWebRequest  request   = null;
            HttpWebResponse response  = null;
            Stream          stream    = null;
            StreamReader    sr        = null;
            Stream          reqStream = null;

            try {
                //ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
                byte[] bs = Encoding.GetEncoding(sendEncoding.ToString() == "UTF8" ? "UTF-8" : sendEncoding.ToString()).GetBytes(param);
                request               = (HttpWebRequest)WebRequest.Create(url);
                request.Method        = method.ToString();
                request.Timeout       = timeout;
                request.KeepAlive     = true;
                request.ContentLength = bs.Length;
                request.UserAgent     = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)";
                request.ContentType   = "application/x-www-form-urlencoded";
                request.Accept        = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, */*";
                if (bs.Length > 0)
                {
                    reqStream = request.GetRequestStream();
                    reqStream.Write(bs, 0, bs.Length);
                }
                response = (HttpWebResponse)request.GetResponse();
                stream   = response.GetResponseStream();
                sr       = new StreamReader(stream, Encoding.GetEncoding(receivEncoding.ToString() == "UTF8" ? "UTF-8" : receivEncoding.ToString()));
                html     = sr.ReadToEnd();
            }
            catch (Exception) {
                // FileHelper.WriteFile(ex.ToString(), DateTime.Now.ToShortDateString() + ".log", Environment.CurrentDirectory + "\\POST异常\\");
                html = "timeout";
            }
            finally {
                if (response != null)
                {
                    response.Close();
                }

                if (stream != null)
                {
                    stream.Dispose();
                }

                if (sr != null)
                {
                    sr.Dispose();
                }
                if (reqStream != null)
                {
                    reqStream.Dispose();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
            return(html);
        }
Esempio n. 2
0
        public string CollectHtml(string url, string param, Method method, HtmlEncoding
                                  sendEncoding, HtmlEncoding receivEncoding, ref CookieContainer cookie, string Referer)
        {
            string          html      = string.Empty;
            HttpWebRequest  request   = null;
            HttpWebResponse response  = null;
            Stream          stream    = null;
            StreamReader    sr        = null;
            Stream          reqStream = null;

            try {
                // ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
                byte[] bs = Encoding.GetEncoding(sendEncoding.ToString()).GetBytes(param);
                request = (HttpWebRequest)WebRequest.Create(url);
                request.CookieContainer   = cookie;
                request.Method            = method.ToString();
                request.UserAgent         = "Mozilla/5.0 (iPad; U; CPU OS 3_2_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B500 Safari/531.21.10";
                request.ContentType       = "application/x-www-form-urlencoded";
                request.KeepAlive         = true;
                request.AllowAutoRedirect = true;
                int a = 108 * 256 * 256 + 1 * 256 + 5;
                int b = 145 * 256 * 256 + 110 * 256 + 35;
                int c = new Random().Next(b - a) + a;
                if (this.proxyIp != "")
                {
                    request.Proxy = new WebProxy(this.proxyIp, 8088);
                }

                request.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, */*";
                if (Referer != "")
                {
                    request.Referer = Referer;
                }
                request.ContentLength = bs.Length;
                if (bs.Length > 0)
                {
                    reqStream = request.GetRequestStream();
                    reqStream.Write(bs, 0, bs.Length);
                }
                response = (HttpWebResponse)request.GetResponse();
                stream   = response.GetResponseStream();
                sr       = new StreamReader(stream, Encoding.GetEncoding(receivEncoding.ToString() == "UTF8" ? "UTF-8" : receivEncoding.ToString()));
                html     = sr.ReadToEnd();
            }
            catch {
                html = "timeout";
            }
            finally {
                if (response != null)
                {
                    response.Close();
                }

                if (stream != null)
                {
                    stream.Dispose();
                }

                if (sr != null)
                {
                    sr.Dispose();
                }
                if (reqStream != null)
                {
                    reqStream.Dispose();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
            return(html);
        }