c#中HttpWebRequest使用代理請求


做一些抓取網頁,投票等等功能,經常會碰到服務器判斷IP限制IP的情況,此時一般在程序中使用代理服務器訪問服務器是一種解決方式(其他方式有冒充IP,或者直接攻擊服務器等等)

免費的代理服務器可以在網上搜索獲得,一般外國的提供較多,國內的免費代理現在很少了。

或者到一些提供代理IP的收費網站購買,比如“萬變IP”,“極光爬蟲代理”等等,如果量不是很大的話,倒是也不算貴。(這些網站還會提供動態的改本機IP的軟件(一般通過VPN改變))

有了代理的IP,就可以在代碼中使用代理抓取或者投票了,這樣不斷的變換代理IP,服務器就無法識別出客戶端請求IP了,就無法阻止采集與投票了。

 

    public static string HttpGet(string url)
    {
        System.Net.ServicePointManager.DefaultConnectionLimit = 50;//這個值默認是2,根據自己的情況修改
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;//SSL/TLS安全通道
                                                                                                //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
        System.Text.Encoding encoding = System.Text.Encoding.UTF8;
        System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
        #region
        request.CachePolicy = new System.Net.Cache.HttpRequestCachePolicy(System.Net.Cache.HttpRequestCacheLevel.NoCacheNoStore);//清緩存
        #region 代理
        if (TP != null)
        {
            WebProxy proxyObject = new WebProxy("58.213.26.166", 36563);//str為IP地址 port為端口號
            //WebProxy proxyObject = new WebProxy("210.14.132.67");//str為IP地址 port為端口號
            request.Proxy = proxyObject; //設置代理 
        }
        #endregion
        //request.Headers.Add("X-Forwarded-For", "162.150.10.16");
        //request.Headers.Add("HTTP_X_FORWARDED_FOR", "162.150.10.16");
        //request.Host = "www.163.com";
        #endregion
        request.Method = "GET";
        request.Accept = "text/html, application/xhtml+xml, */*";
        request.ContentType = "application/json";

        System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
        using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
        {
            return reader.ReadToEnd();
        }

    }

 

更詳細的使用

 

"http://www.domain.com";                            //設定要獲取的地址
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(urlStr);    //建立HttpWebRequest對象
hwr.Timeout 60000;                                                //定義服務器超時時間
WebProxy proxy new WebProxy();                                    //定義一個網關對象
proxy.Address new Uri("http://proxy.domain.com:3128");            //網關服務器:端口
proxy.Credentials new NetworkCredential("f3210316""6978233");    //用戶名,密碼
hwr.UseDefaultCredentials true;                                    //啟用網關認証
hwr.Proxy = proxy;                                                    //設置網關
HttpWebResponse hwrs = (HttpWebResponse)hwr.GetResponse();            //取得回應
Stream s = hwrs.GetResponseStream();                                //得到回應的流對象
StreamReader sr new StreamReader(s, Encoding.UTF8);                //以UTF-8編碼讀取流
StringBuilder content new StringBuilder();                        //
while (sr.Peek() != -1)                                                //每次讀取一行,直到
{                                                                    //下一個字節沒有內容
    content.Append(sr.ReadLine()+""r"n");                            //返回為止
}                                                                    //
return content.ToString() ;                                            //返回得到的字符串

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM