Proxy Basic Authentication in C#: HTTP 407 error

后端 未结 6 1970
伪装坚强ぢ
伪装坚强ぢ 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 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... :)

提交回复
热议问题