HttpClient and using proxy - constantly getting 407
You're setting the proxy credentials in the wrong place.
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.
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);
HTTP Client is not authenticating to proxy server C#
From the response header
Proxy-Authenticate: NTLM
The proxy accepts Windows credential instead of username/password.
Try this instead
var proxy = new WebProxy()
{
...
UseDefaultCredentials = true
/*Credentials = new NetworkCredential(
userName: Settings.ProxyUserName,
password: Settings.ProxyPassword)*/
};
HttpClient. Default credentials to authenticate on auto discovered proxy
You can pass credentials to a default system proxy using code like this with HttpClient:
var handler = new HttpClientHandler();
handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials;
var client = new HttpClient(handler);
Related Topics
How to Use Rsa to Encrypt Files (Huge Data) in C#
How to Set the Color of a Selected Row in Datagrid
Event Signature in .Net -- Using a Strong Typed 'Sender'
How to Access Property of Anonymous Type in C#
What's a Good Way to Overwrite Datetime.Now During Testing
Do the New C# 5.0 'Async' and 'Await' Keywords Use Multiple Cores
Httpclient and Using Proxy - Constantly Getting 407
Design Pattern for Handling Multiple Message Types
Generating an Array of Letters in the Alphabet
Unit Testing ASP.NET MVC Authorize Attribute to Verify Redirect to Login Page
ASP.NET MVC - How to Show Unauthorized Error on Login Page
Difference Between System.Datetime.Now and System.Datetime.Today
Private Inner Classes in C# - Why Aren't They Used More Often
C# Memory Address and Variable