簡體   English   中英

HttpClient如何做代理連接以繞過地理阻止站點C#

[英]HttpClient how to do Proxy Connection to bypass geo blocking sites C#

我正在嘗試使用HttpClient通過這段代碼連接到代理。 我想嚴格使用HttpClient或如果無法連接到代理。 只要完成任務,就可以使用任何c#庫。

        HttpClientHandler handler = new HttpClientHandler();
       //setup web proxy and credentials
        var webproxy = 
            new WebProxy("94.232.55.98", 8080)//ip and port number
        {
            UseDefaultCredentials = false,
            Credentials = CredentialCache.DefaultCredentials
        };

        handler = new HttpClientHandler
        {
            Proxy = webproxy,
            UseProxy = true,
            PreAuthenticate = true,
            UseDefaultCredentials = false
        };

        container = new CookieContainer();
        handler.CookieContainer = container;
        handler.UseCookies = true;
        client = new HttpClient(handler);
        //Query a url and get its contents, but the request was not using any proxy seemingly
        HttpResponseMessage responseMessage = client.GetAsync("https://shop.shoprite.com/").Result;

在查看代碼時,我需要有關如何將httpclient連接到AU代理以及如何獲得具有或不具有憑據的代理以及使其一起工作的指南。

我試圖訪問僅在澳大利亞被阻止的網站,這就是為什么我嘗試使用代理的原因。

提前致謝!

編輯:

我已經從該站點檢索了代理(查看AU代理) https://free-proxy-list.net/ ,我得到了第一個IP並將其放在WebProxy上,以8080作為端口號為例 在此處輸入圖片說明

但它似乎不起作用。

當我要請求該網站時,我遇到這樣的錯誤,即訪問一個除澳大利亞以外其他任何地方都受到地理封鎖的網站。

在此處輸入圖片說明

看起來您的代碼是正確的,但是您嘗試連接的主機無法通過您嘗試使用的代理進行訪問。 您可以通過更改最后幾行以使用EnsureSuccessStatusCode方法來獲得更有用的錯誤消息。 如果狀態代碼不是2XX,則將引發異常。

using (var client = new HttpClient(handler))
using (var responseMessage = await client.GetAsync("http://shop.shoprite.com/"))
{
    responseMessage.EnsureSuccessStatusCode();
    responseMessage.Dump();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM