【问题标题】:Vb.net webclient error : too many automatic redirections was attemptedVb.net webclient 错误:尝试了太多自动重定向
【发布时间】:2017-06-07 10:47:57
【问题描述】:

此代码之前运行良好,但从昨天开始出现错误。 在阅读了许多类似的讨论后,我认为我的问题是关于 cookie 的,所以我添加了一个 CookieContainer 但我仍然得到同样的错误。 我的代码允许我从网站获取图片及其标题并显示它们,这是我的代码:
表格一:

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 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

【问题讨论】:

    标签: vb.net visual-studio webclient cookiecontainer


    【解决方案1】:

    该网站确实添加了一个新功能,该功能可以自动检测 Web 客户端位置(国家/地区)并将它们重定向到另一个网站,所以在我的情况下,我将我的 Web 客户端代理设置为美国,这样它就可以访问我想要的链接而无需重定向。 要添加您可以使用的代理设置:

    webclient1.Proxy = New WebProxy("YourProxyServerName", port)
    

    例如

    webClient.Proxy = New WebProxy("100.12.34.36", 8080)
    

    这里是免费的美国代理列表: https://www.us-proxy.org/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多