Aub Monk
Aub Monk

Reputation: 3

The remote server returned an error (403), VB.NET

Here is my code:

  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

The error is : "The remote server returned an error: (403) Forbidden." I have looked at some other people with the same error but cannot find a specific fix. tia - Aubrey

Upvotes: 0

Views: 1385

Answers (1)

Chillzy
Chillzy

Reputation: 468

You need to specify a web client. ex: 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

Upvotes: 1

Related Questions