Visual Basic

I have managed to get a kind of working app using python, but I really am struggling to get confident using python.
Do you have the equivalent code for logging in, etc for visual basic.net?

I’m still finding python extremely slow going so picked up some code of the internet for VB

Dim request As WebRequest = WebRequest.Create(“https://api.smarkets.com/v3/sessions/”)
request.Method = “POST”
Dim postData As String
postData = “username=” & user & “&password=” & pass
Dim quote = Chr(34)
postData = “{”“password”": “”" + pass + “”","“username”": “”" + user + “”"}"
Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(postData)
request.ContentType = “application/x-www-form-urlencoded”
request.ContentLength = byteArray.Length
Dim dataStream As System.IO.Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim response As WebResponse = request.GetResponse()

I get “Error 400 - Bad request”

Any suggestions?

Hi Michael,

The content type should application/json. I’ve never used VB .Net before, so there may be other issues.

Hope this helps.

Isabel

Hi Isabel
Thanks for your response.
I now get 401 Error - unauthorized

Hi Michael,

You should be able to check the error_type in the response; 401 indicates either PASSWORD_RESET_NEEDED or INVALID_CREDENTIALS.

Isabel

request.getresponse()’ threw an exception of type ‘System.Net.WebException’
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233079
HelpLink: Nothing
InnerException: Nothing
Message: “The remote server returned an error: (401) Unauthorized.”
Response: {System.Net.HttpWebResponse}
Source: “System”
StackTrace: " at System.Net.HttpWebRequest.GetResponse()"
Status: ProtocolError {7}
TargetSite: {System.Net.WebResponse GetResponse()}