C# Web 请求错误 407 需要代理身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16420552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Web Request error 407 Proxy Authentication Required
提问by Sait DURU
Trying to GetResponse From a web site;
尝试从网站获取响应;
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();
}
}
}
This code is working on win7 and LAN and on win8 and any of wireless connection but doesn't work on win8 and LAN error: 407 Proxy authentication required. Is there any solution. Thanks.
此代码适用于 win7 和 LAN 以及 win8 和任何无线连接,但不适用于 win8 和 LAN 错误:需要 407 代理身份验证。有什么解决办法。谢谢。
采纳答案by Damith
try with adding proxy credentials to request and also give network credentials
尝试添加代理凭据以请求并提供网络凭据
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...
Edit
编辑
For requests that you create, you can disable automatic proxy detection at the request level by using a null Proxy with your request
对于您创建的请求,您可以通过在请求中使用空代理来禁用请求级别的自动代理检测
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com/");
request.Proxy = null;
//rest of the code
回答by Pramod
WebProxy webProxy = new WebProxy("http://myproxy.net:8080/", true)
{
UseDefaultCredentials = false,
Credentials = new NetworkCredential("username", "pw")
};
Please note Correct sequence to set property {other wise failed for me}
请注意设置属性的正确顺序{否则我失败了}

