[英]WebClient.DownloadFile requires what exactly for the URI?
I'm good with the code, it works great for other solutions of mine.我对代码很好,它适用于我的其他解决方案。 I have a knowledge gap as I do not understand what constitutes a URI.
我有一个知识空白,因为我不明白什么是 URI。 This should work, but does not:
这应该有效,但不起作用:
https://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download https://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download
Now I'm thinking that this is not a file right?现在我在想这不是一个文件吗? Throwing the above at a browser provides a file though.
不过,将上述内容扔到浏览器会提供一个文件。 The exception message is "The underlying connection was closed: An unexpected error occurred on a receive."
异常消息是“基础连接已关闭:接收时发生意外错误。”
String address = "https://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download";
.....
using (WebClient Client = new WebClient())
{
try
{
Client.DownloadFile(address, destPath + filename);
}
catch (Exception ex)
{
Log.Line("Error: " + ex.Message);
return 1;
}
}
You've got a perfectly valid URI.您有一个完全有效的 URI。 The target server may respond to requests in a different way than you expect though.
目标服务器可能会以与您预期不同的方式响应请求。 For example depending on your web client.
例如,取决于您的网络客户端。 To debug issues like this use
curl
.要调试此类问题,请使用
curl
。
curl -v https://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download
The above command shows you that the server does not reply with the expected csv file.上面的命令显示服务器没有回复预期的 csv 文件。 That's not a problem in your code.
这在您的代码中不是问题。 You can try to pretend a different user agent using the curl -H flag or set some redirection options until you get there.
您可以尝试使用 curl -H 标志假装不同的用户代理或设置一些重定向选项,直到到达那里。
In your specific case it seems to be the header Accept-Encoding: gzip
that solves the issue.在您的特定情况下,标题
Accept-Encoding: gzip
似乎可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.