Proxy Basic Authentication in C#: HTTP 407 error

后端 未结 6 1880
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 05:18

I am working with a proxy that requires authentication, i.e., in a browser if I try to open a page it will immediately ask for credentials. I supplied same credentials in my

相关标签:
6条回答
  • 2020-11-28 05:52

    You can use like this, it works!

            WebProxy proxy = new WebProxy
            {
                Address = new Uri(""),
                Credentials = new NetworkCredential("", "")
            };
    
            HttpClientHandler httpClientHandler = new HttpClientHandler
            {
                Proxy = proxy,
                UseProxy = true
            };
    
            HttpClient client = new HttpClient(httpClientHandler);
    
            HttpResponseMessage response = await client.PostAsync("...");
    
    0 讨论(0)
  • 2020-11-28 06:00

    This method may avoid the need to hard code or configure proxy credentials, which may be desirable.

    Put this in your application configuration file - probably app.config. Visual Studio will rename it to yourappname.exe.config on build, and it will end up next to your executable. If you don't have an application configuration file, just add one using Add New Item in Visual Studio.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.net>
        <defaultProxy useDefaultCredentials="true" />
      </system.net>
    </configuration>
    
    0 讨论(0)
  • 2020-11-28 06:00

    here is the correct way of using proxy along with creds..

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
    
    IWebProxy proxy = request.Proxy;                    
    if (proxy != null)
    {
        Console.WriteLine("Proxy: {0}", proxy.GetProxy(request.RequestUri));
    }
    else
    {
        Console.WriteLine("Proxy is null; no proxy will be used");
    }
    
    WebProxy myProxy = new WebProxy();
    Uri newUri = new Uri("http://20.154.23.100:8888");
    // Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
    myProxy.Address = newUri;
    // Create a NetworkCredential object and associate it with the 
    // Proxy property of request object.
    myProxy.Credentials = new NetworkCredential("userName", "password");
    request.Proxy = myProxy;
    

    Thanks everyone for help... :)

    0 讨论(0)
  • 2020-11-28 06:05

    I was getting a very similar situation where the HttpWebRequest wasn't picking up the correct proxy details by default and setting the UseDefaultCredentials didn't work either. Forcing the settings in code however worked a treat:

    IWebProxy proxy = myWebRequest.Proxy;
    if (proxy != null) {
        string proxyuri = proxy.GetProxy(myWebRequest.RequestUri).ToString();
        myWebRequest.UseDefaultCredentials = true;
        myWebRequest.Proxy = new WebProxy(proxyuri, false);
        myWebRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
    }
    

    and because this uses the default credentials it should not ask the user for their details.

    0 讨论(0)
  • 2020-11-28 06:11

    This problem had been bugging me for years the only workaround for me was to ask our networks team to make exceptions on our firewall so that certain URL requests didn't need to be authenticated on the proxy which is not ideal.

    Recently I upgraded the project to .NET 4 from 3.5 and the code just started working using the default credentials for the proxy, no hardcoding of credentials etc.

    request.Proxy.Credentials = CredentialCache.DefaultCredentials;
    
    0 讨论(0)
  • 2020-11-28 06:19

    try this

            var YourURL = "http://yourUrl/";
    
             HttpClientHandler handler = new HttpClientHandler() 
             { 
                 Proxy = new WebProxy("http://127.0.0.1:8888"), 
                 UseProxy = true, 
             }; 
    
             Console.WriteLine(YourURL); 
    
             HttpClient client = new HttpClient(handler); 
    
    0 讨论(0)
提交回复
热议问题