Authorization error 401 using VB.NET to submit GET to REST API. Works fine in Access VBA

0

I am upgrading some code from VBA (Access) to VB.NET. The code is a simple HTTP GET on a REST API that is running on a PC on my LAN. Both code is running on the same Windows 7 PC. Access is 365 updated. Visual Studio is 2017.

Please use the link to get my short code. The two samples are only about 10 lines each.

This code works in Access VBA:

Dim httpReq As New MSXML2.ServerXMLHTTP
Dim ServiceURLString As String

ServiceURLString = "https://Win7x64:10880/api/v2/companies"

httpReq.SetOption 2, httpReq.GetOption(2) - SXH_SERVER_CERT_IGNORE_CERT_DATE_INVALID
httpReq.Open "GET", ServiceURLString, False, "user", "password"
httpReq.setRequestHeader "Content-Type", "application/json"
httpReq.send
ResponseText = httpReq.ResponseText
Debug.Print ResponseText

This code in VB.NET give error 401 Unauthorized on the request.GetResponse:

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim url As String = "https://Win7x64:10880/api/v2/companies"
        Dim request As WebRequest = WebRequest.Create(url)
        Dim cred As New System.Net.NetworkCredential("user", "password")
        request.Credentials = cred

        request.ContentType = "application/json"
        ServicePointManager.ServerCertificateValidationCallback = AddressOf AcceptAllCertifications

        Dim response As WebResponse = request.GetResponse
    End Sub

    Private Function AcceptAllCertifications(sender As Object, certificate As X509Certificate, chain As X509Chain, sslPolicyErrors As SslPolicyErrors) As Boolean
        Return True
    End Function
 End Class

Error Details

System.Net.WebException HResult=0x80131509
Message=The remote server returned an error: (401) Unauthorized.
at System.Net.HttpWebRequest.GetResponse()

EDIT - Thank-you Dale for reformatting my post and guiding in how to do it.

Jimi: I have made what I believe to be your intended edits and am still having the same issue. Below is the code after those edits.

Imports System.Net
Imports System.Net.Security
Imports System.Security.Cryptography.X509Certificates
Imports System.Text

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ServicePointManager.ServerCertificateValidationCallback = AddressOf AcceptAllCertifications
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

        Dim url As String = "https://Win7x64:10880/api/v2/companies"
        Dim request As HttpWebRequest = WebRequest.Create(url)

        request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes("user" + ":" + "password")))

        request.Credentials = New System.Net.NetworkCredential("user", "password")
        request.ContentType = "application/json"

        Dim response As HttpWebResponse = request.GetResponse

    End Sub

    Private Function AcceptAllCertifications(sender As Object, certificate As X509Certificate, chain As X509Chain, sslPolicyErrors As SslPolicyErrors) As Boolean
        Return True
    End Function
End Class

EDIT 2 Well I have solved the problem but left with questions. The code worked when I added a / character to the url. I know now it's supposed to be there, since that endpoint in a list of elements but the original code did not have it and it worked fine. Perhaps that older serverXMLHTTP technology was more forgiving, and perhaps the API's reporting of errors is not perfect. This is not my API so I won't take it further now I can move on. Thanks for all the help.

vb.net
rest
authentication
asked on Stack Overflow Feb 24, 2019 by Peter • edited Feb 24, 2019 by Peter

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0