我有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
任何想法?
*更新:我已經包含完整的代碼,而不是一個片段。
你能張貼你如何初始化列表2變量請。當我使用上面的代碼時,它對我來說工作正常,但是我根據代碼中的評論對list2做了一個假設。 – theduck 2015-02-25 12:54:38