简体   繁体   English

HttpClient如何做代理连接以绕过地理阻止站点C#

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

I am trying to connect to a proxy via this piece of code using HttpClient. 我正在尝试使用HttpClient通过这段代码连接到代理。 i would like to connect to a proxy strictly using HttpClient, or if unable to. 我想严格使用HttpClient或如果无法连接到代理。 using any c# library as long as the task is achieved. 只要完成任务,就可以使用任何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;

Looking at the code i am in need of guidance on how to connect httpclient to a AU proxy, and how to possibly get a proxy with or without credentials and to make it work all together. 在查看代码时,我需要有关如何将httpclient连接到AU代理以及如何获得具有或不具有凭据的代理以及使其一起工作的指南。

I am trying to access a website geo blocked only in Australia that is why im trying to use a proxy. 我试图访问仅在澳大利亚被阻止的网站,这就是为什么我尝试使用代理的原因。

Thanks in advance! 提前致谢!

EDIT: 编辑:

I have retrieved my proxy from this site ( looking at AU proxies ) https://free-proxy-list.net/ and i get the first ip and put it on WebProxy, having 8080 as port number as an example 我已经从该站点检索了代理(查看AU代理) https://free-proxy-list.net/ ,我得到了第一个IP并将其放在WebProxy上,以8080作为端口号为例 在此处输入图片说明

but it doesnt seem to work. 但它似乎不起作用。

When i am about to request the site, i am having an error like this accessing a site that is geo blocked anywhere else except in australia. 当我要请求该网站时,我遇到这样的错误,即访问一个除澳大利亚以外其他任何地方都受到地理封锁的网站。

在此处输入图片说明

It looks like your code is correct, however the host you are trying to connect to is not accessible through the proxy you are trying to use. 看起来您的代码是正确的,但是您尝试连接的主机无法通过您尝试使用的代理进行访问。 You can get a more useful error message by altering the last few lines to utilise the EnsureSuccessStatusCode method. 您可以通过更改最后几行以使用EnsureSuccessStatusCode方法来获得更有用的错误消息。 This will throw an exception if the status code is not 2XX. 如果状态代码不是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