如何在C#中将代理自动配置脚本信息附加到HttpWebRequest?
我的计算机的代理设置使用自动配置脚本,其地址如下:,我可以正常访问网站,但当我尝试使用HttpWebRequest实例的GetResponse()方法时,如下所示:如何在C#中将代理自动配置脚本信息附加到HttpWebRequest?,c#,httpwebrequest,C#,Httpwebrequest,我的计算机的代理设置使用自动配置脚本,其地址如下:,我可以正常访问网站,但当我尝试使用HttpWebRequest实例的GetResponse()方法时,如下所示: public static string GetContent(string url) { string data = string.Empty; Uri uri=new Uri(url); HttpWebRequest request = (HttpWebRe
public static string GetContent(string url)
{
string data = string.Empty;
Uri uri=new Uri(url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Referer = uri.Host;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.29 Safari/537.36";
request.Method = "GET";
WebProxy proxy = new WebProxy();
proxy.Address = new Uri("http://gazproxy.xxxxxx.com:81/proxy.pac");
request.UseDefaultCredentials = true;
request.Proxy = proxy;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
readStream = new StreamReader(receiveStream);
else
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
data = readStream.ReadToEnd();
response.Close();
readStream.Close();
}
return data;
}
它将抛出异常“远程服务器返回错误:(404)未找到”。我该怎么办?这行代码有问题:
proxy.Address = new Uri("http://gazproxy.xxxxxx.com:81/proxy.pac");
您编写的URI实际上是指一个自动配置脚本,而不是代理服务器。您需要打开并检查该配置脚本,以了解实际的代理服务器是什么。为此,请将该URL复制粘贴到浏览器地址栏上,然后查看/下载或保存该URL的副本
WebProxy对象没有智能去读取一些配置脚本并识别真正的代理服务器
来自MSDN()
WebProxy.Address属性
Address属性包含代理服务器的地址。未启用自动代理检测且未指定自动配置脚本时,Address属性和BypassList将确定用于请求的代理