我正在尝试从网上下载一组文件,我正在编写一个C#控制台应用程序。我正在使用Web客户端来做到这一点。问题是除非我在浏览器中打开文件下载链接,否则无法下载文件。我不知道为什么会这样。我认为这与会话有关。任何帮助,将不胜感激。
这是我的代码
private bool DownloadFile(string pdfId, string filetype)
{
try
{
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile("http://munifilings.com/pdfs/0/" + pdfId + ".pdf", "E:\\Backups\\File" + filetype + "_" + pdfId + ".pdf");
}
return true;
}
catch (Exception ex)
{
Execption = ex.Message;
return false;
}
}
我得到的错误是
“远程服务器返回错误:(404)未找到”
答案 0 :(得分:0)
您的代码似乎正常工作,您是否正确传递了pdf ID?
static void Main(string[] args)
{
string[] List = { "k36062", "k15547", "j63390" };
foreach(string line in List)
{
Console.WriteLine(DownloadFile(line, "pdf"));
}
Console.ReadKey();
}
private static string DownloadFile(string pdfId, string filetype)
{
try
{
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile("http://munifilings.com/pdfs/0/" + pdfId + ".pdf", "C:\\Temp\\files\\" + filetype + "_" + pdfId + ".pdf");
}
return "Downloaded No errors";
}
catch(Exception ex)
{
return ex.Message;
}
}