0

我需要在以下地址下载生成的 csv 文件:

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= &显示=全部

不幸的是,在大多数情况下,尽管当您在浏览器中键入网址时会开始正确下载文件,但仍会发生超时。

我尝试了所有我知道的方法。我当前的代码:

    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");

请帮助并修改代码,以便文件能够正确下载。

4

2 回答 2

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");

如果您不指定“用户代理”,某些服务器会拒绝回答。我不知道为什么会这样,但在这里。

于 2013-04-06T11:10:47.117 回答
0

可以试试这个

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;
    }
于 2013-04-06T11:02:37.587 回答