httpsocket post authorization

I am trying to send a command to an ip camera through HTTPSocket. The command works just fine when I enter it in a browser but I can’t make it work in xojo. The command is:

http://user:pass@1.1.1.1:5001/axis-cgi/com/ptz.cgi?camera=1&pan=30

If I send the command as is with HTTPSocket.Post the camera rejects the command with a 401 Authorization.

Dim xHttp as New HTTPSocket
dim xRet as String
xret=xHttp.post(“http://user:pass@1.1.1.1:5001/axis-cgi/com/ptz.cgi?camera=1&pan=30”, 20)

If I send the username and password via HTTPSocket.SetRequestHeader, I get 400 Bad Request.

Dim xHttp as New HTTPSocket
dim xRet as String
xHttp.SetRequestHeader(“Authorization”, "Basic " + EncodeBase64(“user:pass”))
xret=xHttp.post(“1.1.1.1:5001/axis-cgi/com/ptz.cgi?camera=1&pan=30”,20)

I know I’m missing something very basic here, I just can’t figure it out.

I am not very familiar with the HTTPSocket but suspect you want to be using a GET request i.e.

Dim xHttp as New HTTPSocket
dim xRet as String
xret=xHttp.get(“http://user:pass@1.1.1.1:5001/axis-cgi/com/ptz.cgi?camera=1&pan=30”, 20

tried that too, nogo

Are you sure when you type it into a browser it is not forcing a secure connection? I would be surprised it is being done over http and not https. Try httpSecureSocket and then the get request I gave you before.

It is possible that the server (Camera or NVR) is forcing http requests to secure, this is possible. Your browser can switch protocol to HTTPS but your HTTPSocket cannot.

sorry, that didn’t work either, times out. The camera does not go secure when loggedin in the browser . Https is off on the camera, that’s next after I figure http out. I have used HttpSocket other times for non-password services and it works fine.

Do you have access to the security settings in the camera? Is is configured to only allow certain user agents i.e. know browsers. It may be rejecting requests from your app as it is an unknown user agent.

Implement the AuthenticationRequired event, set the username & password and return true.

After setting and resetting some settings on the camera (nothing changed) , using a Get with SetRequestHeader worked for some reason. Thanks for all your help everybody.

Just for reference the reason I said use get is you were passing the parameters in the URL. Post is used when parameters are passed in a form or in a dictionary object in the case of xojo

A newbie question: “Implement the AuthenticationRequired event, set the username & password and return true.” I can’t find where/how to implement the event. If there were an http control I could select its events, but it’s not a control so I don’t know where the events appear so I can add the code?

You use Addhandler to add event handlers to objects that aren’t controls. It may be simpler to set the Authorization header manually before attempting the connection:

  Dim h As New HTTPSocket
  h.SetRequestHeader("Authorization", "Basic " + EncodeBase64(USERNAME + ":" + PASSWORD))
  Dim data As String = h.post("www.example.net/submit.php",20)

Use AddHandler to connect an event with a method to handle the event.

create your own subclass in the project
implement the events in that subclass
use that instead of a plain httpsocket

Right. Norman’s suggestion is the more mainstream/newbie-friendly approach. Ignore AddHandler for now.

Sorry to necro a thread, but I just ran into similar problems and found the solution.

EncodeBase64(“user:pass”).TRIM

For some reason, EncodeBase64 adds some junk to the end of the string if you view the binary output in the inspector. Trim the string and you’ll be golden.

[quote=436340:@David Zeinz]Sorry to necro a thread, but I just ran into similar problems and found the solution.

EncodeBase64(“user:pass”).TRIM

For some reason, EncodeBase64 adds some junk to the end of the string if you view the binary output in the inspector. Trim the string and you’ll be golden.[/quote]

Without the extra param EncodeBase64 includes line endings

EncodeBase64(“user:pass”,0).TRIM // note the extra “wrap at 0 width which means NEVER” parameter

see http://documentation.xojo.com/api/text/encoding_text/encodebase64.html