您将代理凭据设置在错误的位置。
httpClientHandler.Credentials是在代理已建立连接之后提供给服务器的凭据。 如果您弄错了这些,则可能会收到401或403响应。
您需要设置提供给代理的凭据,否则它将首先拒绝将您连接到服务器。 您提供给代理的凭据可能与您提供给服务器的凭据不同。 如果您弄错了这些,将得到407响应。 您得到的是407,因为您根本没有设置这些设置。
// First create a proxy object
var proxy = new WebProxy
{
Address = new Uri($"http://{proxyHost}:{proxyPort}"),
BypassOnLocal = 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 ser