我有一个直接链接到数据库网站上 PDF 的 URL 列表。自动化下载过程将非常容易,除了我必须通过代理服务器访问网站这一事实。我一直在尝试使用的代码是这样的:
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# 的 Web 协议也很陌生。任何帮助,将不胜感激。