C# HttpClient 和使用代理 - 不断得到 407

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29856543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:19:37  来源:igfitidea点击:

HttpClient and using proxy - constantly getting 407

c#.netproxyhttpclient

提问by dexter

Here is the code:

这是代码:

 HttpClient client = null;
 HttpClientHandler httpClientHandler = new HttpClientHandler()
 {
    Proxy = new WebProxy(string.Format("{0}:{1}", proxyServerSettings.Address, 
    proxyServerSettings.Port),false),
    PreAuthenticate = true,
    UseDefaultCredentials = false,
 };


 this.httpClientHandler.Credentials = new NetworkCredential(proxyServerSettings.UserName, 
                        proxyServerSettings.Password);


 this.client = new HttpClient(this.httpClientHandler);

And when I finally do this:

当我最终这样做时:

HttpResponseMessage httpResponseMessage = this.client.PostAsync(urlToPost, new StringContent(data, Encoding.UTF8, this.mediaType)).Result;

It always throws the

它总是抛出

The remote server returned an error: (407) Proxy Authentication Required.

远程服务器返回错误:(407) 需要代理身份验证。

Which I do not understand for the world of me.

我不明白我的世界。

The same proxy set up works just fine when is configured in IE10 or if I use the HttpWebRequestclass instead

相同的代理设置在 IE10 中配置时工作正常,或者如果我使用HttpWebRequest该类

采纳答案by Daryl

You're setting the proxy credentials in the wrong place.

您在错误的位置设置了代理凭据。

httpClientHandler.Credentials are the credentials you give to the serverafter the proxy has already established a connection. If you get these wrong, you'll probably get a 401 or 403 response.

httpClientHandler.Credentials 是您在代理已经建立连接后提供给服务器的凭据。如果您弄错了这些,您可能会收到 401 或 403 响应。

You need to set the credentials given to the proxy, or it will refuse to connect you to the server in the first place. The credentials you provide to the proxy may be different from the ones you provide to the server. If you get these wrong, you'll get a 407 response. You're getting a 407 because you never set these at all.

您需要设置提供给代理的凭据,否则它首先会拒绝将您连接到服务器。您提供给代理的凭据可能与您提供给服务器的凭据不同。如果您弄错了这些,您将收到 407 响应。您收到 407 是因为您根本没有设置这些。

// First create a proxy object
var proxy = new WebProxy
{
    Address = new Uri($"http://{proxyHost}:{proxyPort}"),
    BypassProxyOnLocal = false,
    UseDefaultCredentials = false,

    // *** These creds are given to the proxy server, not the web server ***
    Credentials = new NetworkCredential(
        userName: proxyUserName,
        password: proxyPassword)
};

// Now create a client handler which uses that proxy
var httpClientHandler = new HttpClientHandler
{
    Proxy = proxy,
};

// Omit this part if you don't need to authenticate with the web server:
if (needServerAuthentication)
{
    httpClientHandler.PreAuthenticate = true;
    httpClientHandler.UseDefaultCredentials = false;

    // *** These creds are given to the web server, not the proxy server ***
    httpClientHandler.Credentials = new NetworkCredential(
        userName: serverUserName,
        password: serverPassword);
}

// Finally, create the HTTP client object
var client = new HttpClient(handler: httpClientHandler, disposeHandler: true);

回答by Darksheao

I found some useful information about this here from social.msdn.microsoft.com. From the replies, tests I made and research into the System.Net.WebProxy Classyou need to pass the proxy credentials into the proxy object, not the HttpClientHandler.

在 social.msdn.microsoft.com 上找到了一些关于此的有用信息。从我的回复、测试和研究System.Net.WebProxy 类,您需要将代理凭据传递到代理对象,而不是 HttpClientHandler。

{
    Proxy = new WebProxy(string.Format("{0}:{1}", proxyServerSettings.Address, 
    proxyServerSettings.Port),false),
    PreAuthenticate = true,
    UseDefaultCredentials = false,
    Credentials = new System.Net.NetworkCredential(proxyServerSettings.UserName, 
                    proxyServerSettings.Password),
};

This actually is meant to authenticate the connection to the proxy, not the HttpClientHandler.

这实际上是为了验证与代理的连接,而不是 HttpClientHandler。

回答by Taran

You need to pass a proxy Handler. try this it worked for me

您需要传递一个代理处理程序。试试这个对我有用

var handler = new HttpClientHandler();
handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials;

var client = new HttpClient(handler);

HttpResponseMessage response = await client.SendAsync();