HttpClient and using proxy - constantly getting 407

121,000

Solution 1

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

Solution 2

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

Solution 3

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 Class you need to pass the proxy credentials into the proxy object, not the 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.

Share:
121,000
dexter
Author by

dexter

Updated on July 05, 2022

Comments

  • dexter
    dexter almost 2 years

    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.

    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 HttpWebRequest class instead

  • Amir Hajiha
    Amir Hajiha over 5 years
    I am getting Invalid URI: The URI scheme is not valid. exception in Mono
  • arik
    arik about 5 years
    I've found this answer simple and fast fix to apply. tnx
  • Maciej Pulikowski
    Maciej Pulikowski over 4 years
    You have to change Address = new Uri($"http://{proxyHost}:{proxyPort}"), . If you get URI error.
  • zackmark15
    zackmark15 over 3 years
    @Daryl how I can make the proxy as optional for every request? for example I have checkbox which is enable proxy or disable
  • Ram
    Ram almost 2 years
    It works for me on desktop and server proxies where server doesn't have internet