Proxy Basic Authentication in C#: HTTP 407 error

后端 未结 6 1999
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  Happy的楠姐
    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.

提交回复
热议问题