HttpClient和使用代理-不断获得407 [英] HttpClient and using proxy - constantly getting 407

查看:456
本文介绍了HttpClient和使用代理-不断获得407的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码:

 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);

当我最终这样做时:

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

它总是抛出


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

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

我不这样做理解我的世界。

Which I do not understand for the world of me.

在IE10中配置或我使用 HttpWebRequest

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

推荐答案

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

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

httpClientHandler.Credentials是在代理已建立连接之后提供给服务器的凭据。如果您输入的内容有误,则可能会收到401或403响应。

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

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

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.

// 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);

这篇关于HttpClient和使用代理-不断获得407的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆