我的计算机代理设置使用自动配置脚本,其地址如下:http://gazproxy.xxxxxx.com:81/proxy.pac,我可以正常访问网站,但是当我尝试使用HttpWebRequest实例的GetResponse()方法时如下:
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)Not Found。”,我该怎么办?
答案 0 :(得分:2)
问题在于这一行:
proxy.Address = new Uri("http://gazproxy.xxxxxx.com:81/proxy.pac");
您编写的URI实际上是指自动配置脚本,而不是代理服务器。您需要打开并检查该配置脚本以了解实际的代理服务器是什么。为此,请在浏览器地址栏上粘贴该URL,然后查看/下载或保存该文件的副本。
WebProxy对象无法读取某些配置脚本并识别真正的代理服务器。
WebProxy.Address属性
Address属性包含代理服务器的地址。如果未启用自动代理检测,并且未指定自动配置脚本,则Address属性和BypassList将确定用于请求的代理。