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:
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:
Private Sub Reboot()
IgnoreBadCertificates()
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 = HttpWebRequest.Create(address)
request.Host = currentDeviceIPAddress
request.Method = "POST"
request.ContentType = "application/json"
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)
Else
Me.Close()
End If
End Sub