远程服务器返回错误(403),VB.NET

时间:2017-12-17 16:31:28

标签: vb.net

这是我的代码:

  cardName = MainMenu.txt_name.Text & "+" & Me.cmb_sets.SelectedItem
        MsgBox(cardName)

        Dim strURL As String = "https://www.cardmarket.com/en/YuGiOh/Products/Singles/Ra+Yellow+Mega+Pack/Yubel+-+The+Ultimate+Nightmare"
        Dim strOutput As String = ""

        Dim wrResponse As WebResponse
        Dim wrRequest As WebRequest = HttpWebRequest.Create(strURL)


        wrResponse = wrRequest.GetResponse()

        Using sr As New StreamReader(wrResponse.GetResponseStream())
            strOutput = sr.ReadToEnd()
            ' Close StreamReader
            sr.Close()
        End Using

错误是:“远程服务器返回错误:(403)禁止。” 我看了一些其他人有相同的错误,但找不到具体的修复方法。 tia - Aubrey

1 个答案:

答案 0 :(得分:1)

您需要指定一个Web客户端。例如:MS Edge,Firefox,Internet Explorer

Imports System.Net.Http

Public Class Form1
    Private Async Sub Form1_LoadAsync(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim res As String = Await (GetPageAsync("https://www.cardmarket.com/en/YuGiOh/Products/Singles/Ra+Yellow+Mega+Pack/Yubel+-+The+Ultimate+Nightmare"))
    End Sub

    Async Function GetPageAsync(ByVal URL As String) As Task(Of String)
        Dim client As New HttpClient
        Dim stroutput As String = ""
        client.DefaultRequestHeaders.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
        Try
            stroutput = Await (client.GetStringAsync(New Uri(URL)))
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
        Return stroutput

    End Function
End Class