3

试图从网站获取响应;

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 代理身份验证。有什么解决办法。谢谢。

4

2 回答 2

8

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

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
于 2013-05-07T13:44:35.073 回答
2
WebProxy webProxy = new WebProxy("http://myproxy.net:8080/", true)
     {
         UseDefaultCredentials = false,
         Credentials = new NetworkCredential("username", "pw")

     };

请注意设置属性的正确顺序{否则对我来说失败}

于 2015-11-19T07:24:27.340 回答