为什么我需要进行代理身份验证'当我在c#中设置代理凭据时

时间:2018-06-10 21:16:36

标签: c# proxy dotnet-httpclient

我发现了几个有关此问题,但它们似乎解决了Windows身份验证问题。我有自定义用户和密码,此代码适用于其他现有代理。现在,自从一周以来,我们使用使用{Basic realm="Private Proxies - Password Auth"}的新代理服务器,但代码不起作用。

        var user = "********";
        var pass = "********";
        var port = "********";
        var ip = "********";
        var url = "********";
        var baseUrl = "********";

        NetworkCredential cr = new NetworkCredential(user, pass);
        cr.Domain = ip;

        WebProxy proxy = new WebProxy(ip + ":" + port) { BypassProxyOnLocal = false, Credentials = cr, UseDefaultCredentials = false};

        HttpClientHandler handler = new HttpClientHandler();

        handler.Proxy = proxy;
        handler.UseProxy = true;

        HttpClient httpClient = new HttpClient(handler);
        httpClient.BaseAddress = new Uri(baseUrl);

        var result = httpClient.GetAsync(url).Result;

        Console.WriteLine(result.Content.ReadAsStringAsync().Result);
        Console.Read();

我已经使用Firefox测试了凭据,并且在我在弹出窗口中提供凭据后代理工作。我在代理中设置了NetworkCredential,但是当我执行请求时,它只返回407: Proxy Authentication Required

我尝试手动设置Proxy-Authorization标头。它仍然无效。

enter image description here

有谁知道如何解决这个问题??

1 个答案:

答案 0 :(得分:0)