Vb.net webclient error : too many automatic redirections was attempted
Vb.net webclient error : too many automatic redirections was attempted
这段代码 运行 之前是没问题的,但从昨天开始出现错误。
在阅读了许多类似的讨论后,我虽然我的问题是关于 cookie,所以我添加了一个 CookieContainer,但我仍然得到同样的错误。
我的代码允许我从网站获取图片及其标题并显示它们,这是我的代码:
表格 1 :
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim websiteURL1 As String = "http://www.gamestop.com/collection/upcoming-video-games"
Class1.getPics(websiteURL1, "<img src=""(?<Data>[^>]*)""><p>(?<Dataa>[^>]*)<br>")
Me.AutoScroll = True
End Sub
Class1 函数:
Public Shared Function getPics(website As String, pattern As String)
Dim tempTitles As New List(Of String)()
Dim tempTitles2 As New List(Of String)()
Dim lestitres As New List(Of titlesclass)
Dim webClient As New CookieAwareWebClient()
webClient.Headers.Add("user-agent", "null")
'If the website happens to go offline, at least your application wont crash.
'Handle default proxy
Dim proxy As IWebProxy = WebRequest.GetSystemWebProxy()
proxy.Credentials = CredentialCache.DefaultCredentials
webClient.Proxy = proxy
Dim content As String = webClient.DownloadString(website)
Dim query = From title In Regex.Matches(content, pattern).Cast(Of Match)
Select New With {Key .Link = String.Concat("http://www.gamestop.com", title.Groups("Data").Value),
Key .Title = title.Groups("Dataa").Value}
Dim titles = tempTitles.Distinct().ToArray() 'remove duplicate titles
Dim titles2 = lestitres.Distinct().ToArray()
lestitres.Clear()
'Count Items
Dim item As Integer = 0
'Count Row
Dim row As Integer = 0
'image: 222*122
For Each letitre In query.Distinct
Dim ImageInBytes() As Byte = webClient.DownloadData(letitre.Link)
Dim ImageStream As New IO.MemoryStream(ImageInBytes)
Dim MyPic As New PictureBox
Dim MyLab As New Label
'x = numéro item fois largeur image
'y = numéro de ligne fois hauteur image
MyPic.Location = New Point(item * 222, row * 122)
MyLab.Location = New Point(item * 222, row * 122)
MyPic.SizeMode = PictureBoxSizeMode.AutoSize
MyLab.Text = letitre.Title
MyPic.Image = New System.Drawing.Bitmap(ImageStream)
Form2.Controls.Add(MyPic)
Form2.Controls.Add(MyLab)
'Bring Labels to front
For Each ctrl As Control In Form2.Controls
'If the control is of type button
If TypeOf ctrl Is Label Then
'Then disable it
ctrl.BringToFront()
End If
Next
'Increment the item count
item = item + 1
'If item is multiple of 4 or could check 4 then
If item Mod 4 = 0 Then
'Reset counter
item = 0
'Increment Row
row = row + 1
End If
Next
End Function
CookieContainer Web 客户端 class :
Imports System.Net
Imports System.Text.RegularExpressions
Public Class CookieAwareWebClient
Inherits WebClient
Private cc As New CookieContainer()
Private lastPage As String
Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As System.Net.WebRequest
Dim R = MyBase.GetWebRequest(address)
If TypeOf R Is HttpWebRequest Then
With DirectCast(R, HttpWebRequest)
.CookieContainer = cc
If Not lastPage Is Nothing Then
.Referer = lastPage
End If
End With
End If
lastPage = address.ToString()
Return R
End Function
End Class
该网站确实添加了一个新功能,可以自动检测 webclient 位置(国家)并将它们重定向到另一个网站,所以在我的例子中,我将我的 webclient 代理设置为美国,这样它就可以访问 link我想没有重定向。
要添加代理设置,您可以使用:
webclient1.Proxy = New WebProxy("YourProxyServerName", port)
例如
webClient.Proxy = New WebProxy("100.12.34.36", 8080)
这是免费的美国代理列表:
https://www.us-proxy.org/
这段代码 运行 之前是没问题的,但从昨天开始出现错误。
在阅读了许多类似的讨论后,我虽然我的问题是关于 cookie,所以我添加了一个 CookieContainer,但我仍然得到同样的错误。
我的代码允许我从网站获取图片及其标题并显示它们,这是我的代码:
表格 1 :
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim websiteURL1 As String = "http://www.gamestop.com/collection/upcoming-video-games"
Class1.getPics(websiteURL1, "<img src=""(?<Data>[^>]*)""><p>(?<Dataa>[^>]*)<br>")
Me.AutoScroll = True
End Sub
Class1 函数:
Public Shared Function getPics(website As String, pattern As String)
Dim tempTitles As New List(Of String)()
Dim tempTitles2 As New List(Of String)()
Dim lestitres As New List(Of titlesclass)
Dim webClient As New CookieAwareWebClient()
webClient.Headers.Add("user-agent", "null")
'If the website happens to go offline, at least your application wont crash.
'Handle default proxy
Dim proxy As IWebProxy = WebRequest.GetSystemWebProxy()
proxy.Credentials = CredentialCache.DefaultCredentials
webClient.Proxy = proxy
Dim content As String = webClient.DownloadString(website)
Dim query = From title In Regex.Matches(content, pattern).Cast(Of Match)
Select New With {Key .Link = String.Concat("http://www.gamestop.com", title.Groups("Data").Value),
Key .Title = title.Groups("Dataa").Value}
Dim titles = tempTitles.Distinct().ToArray() 'remove duplicate titles
Dim titles2 = lestitres.Distinct().ToArray()
lestitres.Clear()
'Count Items
Dim item As Integer = 0
'Count Row
Dim row As Integer = 0
'image: 222*122
For Each letitre In query.Distinct
Dim ImageInBytes() As Byte = webClient.DownloadData(letitre.Link)
Dim ImageStream As New IO.MemoryStream(ImageInBytes)
Dim MyPic As New PictureBox
Dim MyLab As New Label
'x = numéro item fois largeur image
'y = numéro de ligne fois hauteur image
MyPic.Location = New Point(item * 222, row * 122)
MyLab.Location = New Point(item * 222, row * 122)
MyPic.SizeMode = PictureBoxSizeMode.AutoSize
MyLab.Text = letitre.Title
MyPic.Image = New System.Drawing.Bitmap(ImageStream)
Form2.Controls.Add(MyPic)
Form2.Controls.Add(MyLab)
'Bring Labels to front
For Each ctrl As Control In Form2.Controls
'If the control is of type button
If TypeOf ctrl Is Label Then
'Then disable it
ctrl.BringToFront()
End If
Next
'Increment the item count
item = item + 1
'If item is multiple of 4 or could check 4 then
If item Mod 4 = 0 Then
'Reset counter
item = 0
'Increment Row
row = row + 1
End If
Next
End Function
CookieContainer Web 客户端 class :
Imports System.Net
Imports System.Text.RegularExpressions
Public Class CookieAwareWebClient
Inherits WebClient
Private cc As New CookieContainer()
Private lastPage As String
Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As System.Net.WebRequest
Dim R = MyBase.GetWebRequest(address)
If TypeOf R Is HttpWebRequest Then
With DirectCast(R, HttpWebRequest)
.CookieContainer = cc
If Not lastPage Is Nothing Then
.Referer = lastPage
End If
End With
End If
lastPage = address.ToString()
Return R
End Function
End Class
该网站确实添加了一个新功能,可以自动检测 webclient 位置(国家)并将它们重定向到另一个网站,所以在我的例子中,我将我的 webclient 代理设置为美国,这样它就可以访问 link我想没有重定向。 要添加代理设置,您可以使用:
webclient1.Proxy = New WebProxy("YourProxyServerName", port)
例如
webClient.Proxy = New WebProxy("100.12.34.36", 8080)
这是免费的美国代理列表: https://www.us-proxy.org/