2015-02-23 53 views
1

我有5個zip文件我想從網站下載。WebClient正在下載Zip文件404錯誤重定向

http://download.companieshouse.gov.uk/BasicCompanyData-2015-02-01-part1_5.zip http://download.companieshouse.gov.uk/BasicCompanyData-2015-02-01-part2_5.zip http://download.companieshouse.gov.uk/BasicCompanyData-2015-02-01-part3_5.zip http://download.companieshouse.gov.uk/BasicCompanyData-2015-02-01-part4_5.zip http://download.companieshouse.gov.uk/BasicCompanyData-2015-02-01-part5_5.zip

但是如果我使用下面的代碼我得到一個404錯誤,我認爲是HTTP的結果:當我瀏覽//被丟棄到瀏覽器中的頁面,但不是當我使用我的代碼時。

Try 
     Dim reg As String = """.*zip""" 
     Dim list As New List(Of String)() 
     Dim list2 As New List(Of String)() 
     Dim myRegex As New Regex(reg, RegexOptions.None) 
     TextBox1.Text = New System.Net.WebClient().DownloadString("http://download.companieshouse.gov.uk/en_output.html").ToLower 
     For Each myMatch As Match In myRegex.Matches(TextBox1.Text) 
      list.Add(myMatch.Value) 
     Next 
     Dim temp As String 
     For Each i In list 
      temp = i.Remove(0, 1) 
      temp = temp.Remove(temp.Length - 1, 1) 
      list2.Add(temp) 
     Next 
     Dim x As Integer = 1 
     For Each i In list2 
      Dim address As String = "http://download.companieshouse.gov.uk/" + i 
      Dim des As String = Application.StartupPath + "\" + x.ToString + ".zip" 
      Dim client As New System.Net.WebClient() 
      client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)") 
      client.DownloadFile(address, des) 
      x = x + 1 
     Next 

     For i As Integer = 1 To x Step 1 
      Shell(Application.StartupPath + "\7za.exe e " + Application.StartupPath + "\" + x + ".zip") 
     Next 
     list.Clear() 
    Catch ex As Exception 
     MsgBox(ex.ToString) 
    End Try 

任何想法?

*更新:我已經包含完整的代碼,而不是一個片段。

+0

你能張貼你如何初始化列表2變量請。當我使用上面的代碼時,它對我來說工作正常,但是我根據代碼中的評論對list2做了一個假設。 – theduck 2015-02-25 12:54:38

回答

3

這可能是您存儲文件名稱數據的方式。有你的代碼的一個或兩個其他的問題:

Private filList As New List(Of String) From {"BasicCompanyData-2015-02-01-part1_5.zip", 
             "BasicCompanyData-2015-02-01-part2_5.zip", 
             "BasicCompanyData-2015-02-01-part3_5.zip", 
             "BasicCompanyData-2015-02-01-part4_5.zip", 
             "BasicCompanyData-2015-02-01-part5_5.zip"} 

然後在其他地方,如按鈕點擊:

Dim destPath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) 
Dim destFile As String 
Dim baseURL As String = "http://download.companieshouse.gov.uk/" 
Dim thisURL As String 

Using wc As New WebClient 
    wc.Headers.Add("user-agent", 
      "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)") 

    For Each f As String In filList 
     thisURL = baseURL & f 
     destFile = Path.Combine(destPath, f) 
     wc.DownloadFile(thisURL, destFile) 
    Next 

End Using 
  1. using塊確保Web客戶端將被關閉,處置和資源釋放。
  2. 在VS中,使用Application.StartupPath可以工作,但是作爲部署的應用程序,當應用程序安裝到Program Files...時可能會失敗,因爲您的應用程序可能無法在該應用程序中編寫。使用Environment.GetFolderPath可以獲得文件夾,如MyDocuments
  3. 該版本保留了原始文件名,因此如果您正在執行其他文件,它們不會相互覆蓋(使用App StartupPath時可能會出現的另一個問題)。
0

問題出在我找到頁面上的鏈接的方式,我正在閱讀網頁到文本框中,並將.toLower應用於它,以便在我提取網址時出現錯誤。

問題行:

TextBox1.Text = New System.Net.WebClient().DownloadString("http://download.companieshouse.gov.uk/en_output.html").ToLower