2013-04-06 74 views
1

我需要下載生成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=&show=all

不幸的是,在大多數情況下,發生超時,儘管事實上,當你鍵入一個網址在瀏覽器中開始正確下載文件。

我嘗試了我所知道的所有方法。我目前的代碼:

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

請幫助和修改代碼,以便該文件可以正確下載。

回答

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

有些服務器拒絕回答是否你沒有指定你的「用戶代理」。我不知道爲什麼這樣,但在這裏。

+0

非常感謝。 你的幫助解決了這個問題。誤導我幾個小時前正常工作。 – codemenaer 2013-04-06 11:31:39

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; 
    } 
+0

不幸的是,我得到連接超時。 問題可能在於查詢。我想我必須處理「POST」。但不太清楚。 – codemenaer 2013-04-06 11:07:37

+0

試試這個WebClient.DownloadFileAsync()。你可以用你自己的超時時間通過定時器來調用CancelAsync()。 – Darshan 2013-04-06 11:17:53

+0

檢查我編輯了我的答案 – Darshan 2013-04-06 11:18:49