【发布时间】:2015-02-23 03:36:56
【问题描述】:
我想从网站下载 5 个 zip 文件。
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
有什么想法吗?
*更新:我已包含完整代码而不是 sn-p。
【问题讨论】:
-
能否请您发布您如何初始化 list2 变量。当我使用上面的代码时,它对我来说很好,但我根据您在代码中的评论对 list2 做出了假设。
标签: .net vb.net download zip webclient