Click here to Skip to main content
15,887,283 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello!
I'm trying to connect my vb.net project to an external machine via api, to get and send data.

The objetive will be reboot, poweroff, check status and open door.

I have this code working on a similar equipment with 0 problems.

In this one, every time I try to connect I get the error (400) Bad Request.

Can anyone give some ideas?

Thank you.

What I have tried:

VB.NET
Dim currentDeviceIPAddress As String = "127.0.0.1:8081"
    Dim credentials As String = Convert.ToBase64String(Encoding.ASCII.GetBytes("admin" & ":" & "admin"))

    Public Class JSON_ResetInfoReturn
        <JsonProperty("code")>
        Public Property code As Integer

        <JsonProperty("messages")>
        Public Property messages As String
    End Class

    Public Class JSON_ResetInfo
        <JsonProperty("mode")>
        Public Property mode As String
    End Class



    Public Shared Sub IgnoreBadCertificates()
        System.Net.ServicePointManager.ServerCertificateValidationCallback = New System.Net.Security.RemoteCertificateValidationCallback(AddressOf AcceptAllCertifications)
    End Sub
    Private Shared Function AcceptAllCertifications(ByVal sender As Object, ByVal certification As System.Security.Cryptography.X509Certificates.X509Certificate, ByVal chain As System.Security.Cryptography.X509Certificates.X509Chain, ByVal sslPolicyErrors As System.Net.Security.SslPolicyErrors) As Boolean
        Return True
    End Function


Sub:
VB
Private Sub Reboot()
    'Reboot 

    IgnoreBadCertificates()

    'Send parameters
    Dim initReboot As New JSON_ResetInfo
    With initReboot
        .mode = "Reboot"
    End With

    Dim rebootJsonString As String = String.Empty
    rebootJsonString = JsonConvert.SerializeObject(initReboot)

    Dim request As HttpWebRequest
    Dim address As Uri
    Dim postStream As Stream = Nothing
    Dim result As JSON_ResetInfoReturn


    address = New Uri("https://" + currentDeviceIPAddress + "/api/util/reset")
    'request = WebRequest.CreateHttp(address)
    request = HttpWebRequest.Create(address)
    request.Host = currentDeviceIPAddress
    request.Method = "POST"
    request.ContentType = "application/json"

    ' Login info
    'request.Credentials = New NetworkCredential("admin", "admin")
    request.Headers.Add("Authorization", "Basic " & credentials)

    Dim jsonBytes = Encoding.UTF8.GetBytes(rebootJsonString)
    request.ContentLength = jsonBytes.Length
    postStream = request.GetRequestStream()
    postStream.Write(jsonBytes, 0, jsonBytes.Length)

    Dim responseContent As String = ""

    Using response = TryCast(request.GetResponse(), System.Net.HttpWebResponse)
        Using reader = New System.IO.StreamReader(response.GetResponseStream())
            responseContent = reader.ReadToEnd()
            result = JsonConvert.DeserializeObject(Of JSON_ResetInfoReturn)(responseContent)
        End Using
    End Using


    If result.code > 0 Then
        MessageBox.Show("Error:" + result.code)
        'returnValue = False
    Else
        Me.Close()
    End If

End Sub
Posted
Comments
Dave Kreskowiak 4-Feb-24 17:00pm    
You're getting Bad Request because what you're sending in your request, or NOT sending, isn't valid as far as the server is concerned.

For example, you could be missing a header the server is expecting, or sending a header value that isn't valid. You could be requesting a POST on a method that doesn't accept POST, you could be passing a return content type the server doesn't support, ...

As a side, if you create a HttpWebRequest object passing in a URI, you do not need to set the Host property on it.
Richard Deeming 5-Feb-24 5:22am    
As Dave said, a "bad request" response means the server doesn't recognise what you've sent it.

Unfortunately, nobody here can help you, since we have no access to the server you're talking to, and no idea what it expects you to send.

You'll need to talk to the people who wrote the service to find out what you're doing wrong.

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