Web请求错误407需要代理身份验证

时间:2013-05-07 13:39:39

标签: c# asp.net httpwebrequest lan

尝试从网站获取GetResponse;

using System.Text;
using System.Net;
using System.IO;

namespace DutyPharmacy751013
{
class Program
{
    static void Main(string[] args)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com/");

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        Encoding encoding = Encoding.GetEncoding(response.CharacterSet);

        Stream stream = response.GetResponseStream();
        StreamReader reader = new StreamReader(stream, encoding);
        string responseText= reader.ReadToEnd();
    }
}
}

此代码适用于win7和LAN  以及win8和任何无线连接 但不适用于win8和LAN错误:407需要代理身份验证。 有什么解决方案吗? 感谢。

2 个答案:

答案 0 :(得分:7)

尝试添加代理凭据以请求并提供网络凭据

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com/");
request.Credentials = new NetworkCredential("username", "pw");

WebProxy webProxy = new WebProxy("http://myproxy.net:8080/", true)
         {
             Credentials = new NetworkCredential("username", "pw"),
             UseDefaultCredentials = false
         };

request.Proxy = webProxy;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

//rest of the code...

修改

对于您创建的请求,您可以通过在请求中使用空代理来禁用请求级别的自动代理检测

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com/");
request.Proxy = null;
//rest of the code

答案 1 :(得分:2)

WebProxy webProxy = new WebProxy("http://myproxy.net:8080/", true)
     {
         UseDefaultCredentials = false,
         Credentials = new NetworkCredential("username", "pw")

     };
  

请注意正确的顺序设置属性{其他明智的失败}}