Click here to Skip to main content
15,612,565 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have create rest api for get method and deployed in public server, when i call rest api using below code i will get error "The remote server returned an error: (403) Forbidden."

Dim wc As WebClient
wc = New WebClient
  Dim res As Boolean  
  wc.Headers.Add("x-api-key", "JKL4Or9GF0")
  res = wc.DownloadString("http://localhost:56590/Home/ValidName?LastName=Vogel")
  If res Then
    ' with result...
  End If
Catch ex As Exception
  '...handle error...
End Try

What I have tried:

I will tried with Chilkat dll for call that api, it works and i get return value, in that i set TSL as false, but that dll need to purchase,so i moved ordinary vb code, so in above code how to set tsl as false.

Below the code which is working with chilkat

Dim rest As New Chilkat.Rest
            Dim success As Boolean
            Dim bTls As Boolean = False
            Dim port As Integer = 80
            Dim bAutoReconnect As Boolean = True
            success = rest.Connect("http://localhost:56590/", port, bTls, bAutoReconnect)
            If (success <> True) Then

                '  Debug.WriteLine(rest.LastErrorText)
                Exit Sub
            End If
            rest.AddHeader("x-api-key", "JKL4Or9GF0")
            success = rest.FullRequestNoBodySb("GET", "http://localhost:56590/Home/ValidName?LastName=Vogel", sbResponseBody)

            If (success <> True) Then
                lblmsg1.Text = "2" & rest.LastErrorText
                ' Debug.WriteLine(rest.LastErrorText)
                Exit Sub
            End If

            Dim respStatusCode As Integer = rest.ResponseStatusCode
            If (respStatusCode >= 400) Then

                lblmsg1.Text = "Response Status Code = " & respStatusCode
                'Debug.WriteLine("Response Status Code = " & respStatusCode)
                'Debug.WriteLine("Response Header:")
                'Debug.WriteLine("Response Body:")
                Exit Sub
            End If

            Dim jsonResponse As New Chilkat.JsonObject

Note: For security purpose i changed the url, is there any alternative vb code for call rest api instead of using chilkat ?

Updated 4-Feb-21 17:59pm
Richard Deeming 5-Feb-21 5:16am    
You'd need to trace the request and response to see what the differences are.

You could try using RestSharp[^], or the built-in HttpClient class[^], both of which are free. But until you know what's missing from your WebClient request, it's difficult to know how to fix it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900