此代码之前运行正常,但从昨天开始出现错误。
在阅读了许多类似的问题之后我虽然我的问题是关于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 fucntion:
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 WebClient类:
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
答案 0 :(得分:0)
该网站确实添加了一个新功能,可自动检测webclient位置(国家/地区)并将其重定向到另一个网站,因此在我的情况下,我将我的webclient代理设置为美国,以便它可以访问我想要的链接而无需重定向。 要添加代理设置,您可以使用:
webclient1.Proxy = New WebProxy("YourProxyServerName", port)
例如
webClient.Proxy = New WebProxy("100.12.34.36", 8080)
以下是免费美国代理商列表: https://www.us-proxy.org/