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

来源:互联网 发布:招聘新媒体美工的要求 编辑:程序博客网 时间:2024/05/21 16:52

我们在爬网时经常会遇到

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

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

原创粉丝点击