我有一个直接链接到数据库网站上的PDF的URL列表。除非我必须通过代理服务器访问网站,否则自动执行下载过程将非常容易。我一直在试图使用的代码一直是这个:通过代理下载PDF
public void Download()
{
WebClient wb2 = new WebClient();
WebProxy proxy = new WebProxy("PROXY_URL:port", true);
proxy.Credentials = new NetworkCredential("USERNAME", "PASSWORD");
GlobalProxySelection.Select = proxy;
try
{
for(int i = 0; i < URLList.Length; i++)
{
byte[] Data = DownloadData(URLList[i]);
FileStream fs = new FileStream(@"D:\Files\" + i.toString() + ".pdf", FileMode.Create)
fs.Write(Data, 0, Data.Length);
fs.Close();
}
}
catch(WebException WebEx)
{
MessageBox.Show(WebEx.Message);
}
}
public byte[] DownloadData(string path)
{
WebClient wb2 = new WebClient();
wb2.Credentials = new NetworkCredential("USERNAME","PASSWORD");
return wb2.DownloadData(path);
}
出于某种原因,它返回错误“(400):错误的请求”每一次。我明显能够通过Firefox获得这些PDF,所以我想知道我在这里做错了什么。一般来说,我对编程相当陌生,对于通过C#的网络协议来说,这是一个新手。任何帮助,将不胜感激。
您是否重新检查了URLList。我的意思是,如果它有错别字或者特殊字符干扰.... – loxxy
没有特殊字符,并且当粘贴到Firefox或IE时URL可以正常工作。 – Agni451