欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#遠程html數(shù)據(jù)抓取實例分享

 更新時間:2013年12月03日 10:09:17   作者:  
這篇文章主要介紹了c#遠程html數(shù)據(jù)抓取的方法,大家參考使用吧

復(fù)制代碼 代碼如下:

/// <summary>
        /// 獲取遠程html
        /// </summary>
        /// <param name="url"></param>
        /// <param name="methed"></param>
        /// <param name="param"></param>
        /// <param name="html"></param>
        /// <returns></returns>
        public static bool GetHttp(string url, string methed, string param, out string html)
        {
            methed = methed.ToLower();

            if (param != null && methed == "get" && param.Length > 0)
            {
                url += "?" + param;
            }

            try
            {
                MSXML2.XMLHTTP mx = new MSXML2.XMLHTTPClass();

                mx.open(methed, url, false, null, null);

                if (param != null && methed == "post" && param.Length > 0)
                {
                    mx.setRequestHeader("Content-Length", param.Length.ToString());
                    mx.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                }

                mx.send(param);

                if (mx.readyState != 4)
                {
                    html = "遠程連接失?。?4";
                    return false;
                }
                html = mx.responseText;
                return true;
            }
            catch (Exception ex)
            {
                html = "遠程連接失?。?+ex.Message;
                return false;
            }
        }

        public static bool GetHttp1(string url, string methed, string param, string referer, string encode, out string html)
        {
            //return GetHttp(url,methed,param,out html);

            //string encode = "utf-8";
            //string methed = sendType.ToString();

            if (param != null && methed == "get" && param.Length > 0)
            {
                if (url.IndexOf("?") >= 0)
                {
                    url += "&" + param;
                }
                else
                {
                    url += "?" + param;
                }
            }

            try
            {
                HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(url);

                webreq.Proxy=null;
                webreq.Timeout = 1000 * 6;
                webreq.ContentType = "application/x-www-form-urlencoded";
                webreq.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0";

                //webreq.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)";

                //谷歌的:User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
                //火狐的:User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0
                //標準格式為: 瀏覽器標識 (操作系統(tǒng)標識; 加密等級標識; 瀏覽器語言) 渲染引擎標識 版本信息

                //webreq.AllowAutoRedirect = false;

                //頻繁請求一個網(wǎng)址時,過段時間就會出現(xiàn)“基礎(chǔ)連接已經(jīng)關(guān)閉”
                //webreq.KeepAlive = false;
                //webreq.ProtocolVersion = HttpVersion.Version10;

                if (referer.Length > 0)
                {
                    webreq.Referer = referer;
                }

                CookieContainer mycookies = new CookieContainer();
                webreq.CookieContainer = mycookies;

                //if (this.cookieList != null)
                //{
                //    webreq.CookieContainer.Add(this.GetCookies(webreq.RequestUri, this.cookieList));
                //}

                webreq.Method = methed;

                //post 開始
                if (param != null && methed == "post")
                {
                    byte[] arrbyte = Encoding.GetEncoding(encode).GetBytes(param);
                    webreq.ContentLength = arrbyte.Length;

                    Stream newStream = webreq.GetRequestStream();
                    newStream.Write(arrbyte, 0, arrbyte.Length);
                    newStream.Close();
                }
                //post 結(jié)束

 
                WebResponse w = webreq.GetResponse();

                //返回HTML
                using (HttpWebResponse webres = (HttpWebResponse)webreq.GetResponse())
                {
                    using (Stream dataStream = webres.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding(encode)))
                        {
                            html = reader.ReadToEnd();
                            //this.cookieList = webreq.CookieContainer.GetCookies(webreq.RequestUri);
                            webreq.Abort();//可能會解決卡住或阻塞問題
                        }
                    }
                }
            }
            catch (Exception ex)
            {

                html = "出現(xiàn)異常(HttpHelper.GetHTML),遠程連接失?。? + ex.Message + " url:" + url;
                //System.Windows.Forms.MessageBox.Show(html);
                return false;
            }

            return true;
        }

相關(guān)文章

最新評論