Click here to Skip to main content
15,113,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
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."

VB
Dim wc As WebClient
wc = New WebClient
Try
  Dim res As Boolean  
  wc.Headers.Add("x-api-key", "JKL4Or9GF0")
  res = wc.DownloadString("http://localhost:56590/Home/ValidName?LastName=Vogel")
  If res Then
    '...work 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

VB
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(rest.ResponseHeader)
                'Debug.WriteLine("Response Body:")
                'Debug.WriteLine(sbResponseBody.GetAsString())
                Exit Sub
            End If


            Dim jsonResponse As New Chilkat.JsonObject
            jsonResponse.LoadSb(sbResponseBody)



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

Regards,
Aravind
Posted
Updated 4-Feb-21 18:59pm
v4
Comments
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