我需要在以下地址下载生成的csv文件:
不幸的是,在大多数情况下,尽管您在浏览器中键入网址时开始正确下载文件,但仍会发生超时。
我尝试了所有我认识的方式。我目前的代码:
WebClient webClient = new WebClient();
webClient.DownloadFile("http://www.mbank.pl/bin/sfi/sfi_do_csv.php?sh=0,3,4,5,6,7,8,9,10,11&date=2013-04-05&sorted=StopaZw1r_down&fund_type=&collection=&show=all", @"d:\myfile.csv");
请帮助并修改代码,以便正确下载文件。
答案 0 :(得分:1)
可以试试这个
using (var client = new WebClient())
{
client.DownloadFile("http://www.mbank.pl/bin/sfi/sfi_do_csv.php?sh=0,3,4,5,6,7,8,9,10,11&date=2013-04-05&sorted=StopaZw1r_down&fund_type=&collection=&show=all", @"D:\Downloads\1.zip");
}
或者,如果有任何例外,那就是
public void DownloadFile(string _URL, string _SaveAs)
{
try
{
System.Net.WebClient _WebClient = new System.Net.WebClient();
// Downloads the resource with the specified URI to a local file.
_WebClient.DownloadFile(_URL, _SaveAs);
}
catch (Exception _Exception)
{
// Error
Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
}
}
或者尝试这样做
public class WebDownload : WebClient
{
private int _timeout;
/// <summary>
/// Time in milliseconds
/// </summary>
public int Timeout
{
get
{
return _timeout;
}
set
{
_timeout = value;
}
}
public WebDownload()
{
this._timeout = 60000;
}
public WebDownload(int timeout)
{
this._timeout = timeout;
}
protected override WebRequest GetWebRequest(Uri address)
{
var result = base.GetWebRequest(address);
result.Timeout = this._timeout;
return result;
}
答案 1 :(得分:1)
WebClient webClient = new WebClient();
webClient.Headers.Add("user-agent", "CustomClient");
webClient.DownloadFile("http://www.mbank.pl/bin/sfi/sfi_do_csv.php?sh=0,3,4,5,6,7,8,9,10,11&date=2013-04-05&sorted=StopaZw1r_down&fund_type=&collection=&show=all", @"d:\myfile.csv");
如果您未指定“用户代理”,某些服务器会拒绝回答。我不知道为什么会这样,但在这里。