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

问题描述 投票:0回答:1

这是我的代码:

  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)禁止。”我看了一些其他人有相同的错误,但找不到具体的修复方法。蒂亚 - 奥布里

vb.net
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.