Trying to Post to API via VB.net to directions gives me HttpRequestException

Hello, im trying to access the API with the following Sub for testing purposes. Running the code gives me an exception when calling request.GetResponse(). I copied the code from the examples but it contains some syntax errors so i had to adapt it a bit.

  • {System.Net.WebException: An error occurred while sending the request. The server returned an invalid or unrecognized response. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.IO.IOException: The server returned an invalid or unrecognized response. at System.Net.Http.HttpConnection.FillAsync() at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed) at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) --- End of inner exception stack trace --- at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at System.Net.HttpWebRequest.SendRequest() at System.Net.HttpWebRequest.GetResponse() — End of inner exception stack trace — at System.Net.HttpWebRequest.GetResponse() at openroutedemo.Program.CreateJsonRequest() in C:\Users\PEL\source\repos\openroutedemo\openroutedemo\Program.vb:line 57}`

Public Sub CreateJsonRequest()

    Dim request = TryCast(System.Net.WebRequest.Create("https://api.openrouteservice.org/v2/directions/driving-car/json"), System.Net.HttpWebRequest)

    request.Method = "POST"
    request.Accept = "application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8"
    request.Headers.Add("Authorization", "xxxxMYTOKENxxxx")
    request.Headers.Add("Content-Type", "application/json, application/geo+json; charset=utf-8")

    Dim myobj = New MyObject() With {
.Coordinates = New List(Of List(Of Double)) From {
    New List(Of Double)({8.681495, 49.41461}),
    New List(Of Double)({8.686507, 49.41943}),
    New List(Of Double)({8.687872, 49.420318})
}



    Using writer = New StreamWriter(request.GetRequestStream())
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(myobj))
        Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(myobj).ToString)
        request.ContentLength = byteArray.Length
        writer.Write(byteArray)
        writer.Close()
    End Using

    Dim responseContent As String
    Try
        Dim response = TryCast(request.GetResponse(), HttpWebResponse)
        Using reader = New System.IO.StreamReader(response.GetResponseStream())
            responseContent = reader.ReadToEnd()
            responseContent.ToString()
        End Using
    Catch ex As WebException
        Console.WriteLine(ex.InnerException.Message)
    End Try

End Sub

Best regards

Hi @peznan

there’s no one in our group who uses VB, so we can’t provide any feedback on code. To be able to see if it is something from the openrouteservice API, we would need to know what the response from the API actually is as currently, the error log just reports that VB didn’t get a response it was expecting

Thanks for your help. I managed to get it running by adding the following lines and implementing a friend class helping me to build the request:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
request.PreAuthenticate = True

Feel free to contact me if you need help adapting your demo code.

Best regards