-1

I am trying to consume an API that requires I sign in with a user name and password.

The error I am receiving is

The remote server returned an error: (403) Forbidden.

The Authentication type is Basic.

Public Function ForStackOverFlow(requestUri As String)

    Dim response As String

    Dim baseUri = $"http://someapi.example.com/api"
    Dim URI As Uri = New Uri(requestUri)
    Dim credentialCache As New CredentialCache()

    Dim username As String = "username"
    Dim password As String = "password"

    credentialCache.Add(New System.Uri(baseUri), "Basic", New NetworkCredential(username, password))

    Dim request = WebRequest.Create(URI)
    request.ContentType = "application/x-www-form-urlencoded"
    request.Method = "GET"
    request.Credentials = credentialCache

    Using responseStream = request.GetResponse.GetResponseStream
        Using reader As New StreamReader(responseStream)
            response = reader.ReadToEnd()
        End Using
    End Using

    Return response

End Function

Some resources I accessed in trying to solve this problem.

https://learn.microsoft.com/en-us/dotnet/api/system.net.httprequestheader?view=net-6.0

How to consume an asp.net web api in vb.net application

vb.net Task(Of HttpResponseMessage) to HttpResponseMessage

Value of type 'System.Threading.Tasks.Task(Of String)' cannot be converted to 'String'

SSL TLS 1.2 Channel creation VB.net HTTPS WebRequest

Accept self-signed TLS/SSL certificate in VB.NET

Could not establish trust relationship for the SSL/TLS secure channel: The remote certificate is invalid according to the validation procedure

3
  • What is the API authentication type? JWP? Cookie? basic? Commented Feb 4, 2022 at 19:22
  • The Authentication type is Basic, I edited the question to make it more clear. Commented Feb 4, 2022 at 19:30
  • Very few authenticated APIs will allow raw http access. You probably need https://. Very few APIs also use basic authentication. There's almost always some kind of token you need to generate. Commented Feb 4, 2022 at 20:32

1 Answer 1

1

I suggest using HttpClient and also Async/Await:

e.g

    Public Async Function ForStackOverFlow(requestUri As String) As Task(Of String)
        Using client As New HttpClient()
            Dim URI As Uri = New Uri(requestUri)
            Dim auth = Encoding.ASCII.GetBytes("username:password1234")
            client.DefaultRequestHeaders.Authorization = New Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(auth))
            Dim response = Await client.GetAsync(URI)
            Dim content = response.Content
            Return Await content.ReadAsStringAsync()
        End Using
    End Function

Usage:

Dim result = Await ForStackOverFlow("http://SomeURL")
' or 
' Dim result = ForStackOverFlow("http://SomeURL").Result
Sign up to request clarification or add additional context in comments.

9 Comments

at Dim response = Await client.GetAsync(New Uri(requestUri)) I get Object reference not set to an instance of an object "This exception was originally thrown at this call stack: [External Code]" I tried to wrap in a try catch but it just enters break mode.
two questions. 1- what dotnet version are you using? 2- make sure to pass a correct URL.
4.7.2. I verified the URL is just like the one that is displayed in swagger when I tested the call I am trying to make.
I've updated the answer. also tested locally now. this version should work as expected. if didn't, paste the error message
I tried turning on different exception settings, but all it says is "System.NullReferenceException" Then it says This Exception was originally thrown at this call stack: [External Code]. When I sign in on Swagger it says Authorization Basic, so I would assume we have the auth type correct.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.