URLConnection Equivalent to HTTPSSocket.SetFormData

I’m currently using an HTTPSSocket for posting and getting some data from a website. The first thing I have to do is log in to the site. In the HTTPSSocket, I set up a dictionary with my username and passwords. I then send that dictionary to the HTTPSSocket.SetFormData method. URLConnection has no such method for whatever reason.

I have tried to figure out what that method actually creates so I can do it manually myself, but looking at the socket in the debugger, I can’t actually find out what the SetFormData method does. Here’s what I send with the HTTPSSocket:

[code]Dim d As New Dictionary

d.Value(“Email”) = somevalue
d.Value(“Password”) = someothervalue

Dim gcurl As String = kgcURLStart+“api/account/login”

In URLConnection I only have the SetRequestContent method. I’m assuming I would do something like this in that method:

Dim LoginText as string = “Email=somevalue&Password=someothervalue MyURLConnection.SetRequestContent(LoginText, ??????) MyURLConnection.Send(“POST”, kgcURLStart+"api/account/login”)
The question marks, ??? in the SetRequestContent line are because I have no idea of the Mime Type to be used here. None of the documentation for either socket talks about that.

What would I use?


The 2nd code sample in the Sample Code section of the URLConnection.SetRequestContent page shows how to send form data and also shows that you’d use the Mime type of “application/x-www-form-urlencoded”.

I thought that might be the case, but I wasn’t absolutely positive on the Mime Type. And since there’s no way to view the string that the HTTPSSocket.SetFormData method generates, I couldn’t cross check and tell for sure.

And there’s an error/bug in the Post Button action event. I just filed a bug report on it.

Thanks, Jon. I’ve fixed this for 2019r1.

Great, Paul. Thanks for the quick fix!

And I have it working with the website I’m pulling data from. So all is good.

The nice thing about the URLConnection object as opposed to the HTTPSSocket is that the URLConnection raises an error when there’s no internet connection. The HTTPSSocket just sits there and does nothing.