HttpWebRequest The remote server returned an error: (407) Proxy Authentication

我们在爬网时经常会遇到

 The remote server returned an error: (407) Proxy Authentication

经过查处最后处理的代码如下:

  HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Proxy = WebProxy.GetDefaultProxy();
                request.Proxy.Credentials = CredentialCache.DefaultCredentials;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string responseText = string.Empty;
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    responseText = sr.ReadToEnd();
                }
                response.Close();
                request.Abort();
                return responseText;


网上还有其他方法如下:

webRequest = (HttpWebRequest)HttpWebRequest.Create(address); 
webRequest.Credentials = myCreds; 
webRequest.PreAuthenticate = true; 
webRequest.Method = "HEAD"; 
webRequest.Timeout = 10000; 
webRequest.AllowWriteStreamBuffering = true;     
#region Proxy settings 
WebProxy proxy = new WebProxy(proxyAddress, proxyPort); 
proxy.Credentials = new NetworkCredential(cm.FirewallUser, cm.FirewallPassword); 
webRequest.Proxy = proxy; 
proxy.Credentials = NetworkCredential("domain\\user", "password") 
webRequest.Proxy = proxy; 
webResponse = (HttpWebResponse)webRequest.GetResponse(); 

我没有测试不知道行不行。

你可能感兴趣的:(HttpWebRequest The remote server returned an error: (407) Proxy Authentication)