httpsocket post authorization

  1. 5 years ago

    Mark P

    27 Jan 2014 Answer Victoria BC

    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 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.

  2. Mike C

    27 Jan 2014 (England, Midlands)

    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

  3. Mark P

    27 Jan 2014 Victoria BC

    tried that too, nogo

  4. Mike C

    27 Jan 2014 (England, Midlands)

    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.

  5. Mark P

    27 Jan 2014 Victoria BC

    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.

  6. Mike C

    27 Jan 2014 (England, Midlands)

    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.

  7. Wayne G

    27 Jan 2014 Pre-Release Testers, Xojo Pro New Zealand axisdirect.nz

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

  8. Mark P

    27 Jan 2014 Victoria BC

    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.

  9. Mike C

    28 Jan 2014 (England, Midlands)

    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

  10. 3 years ago

    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?

  11. Andrew L

    9 Feb 2016 San Francisco, CA, USA

    @Peter V 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)
  12. Tim H

    9 Feb 2016 Pre-Release Testers Portland, OR USA

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

  13. Norman P

    9 Feb 2016 Xojo Inc, Pre-Release Testers, Xojo Pro Seeking work. npalardy@great-w...

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

  14. Tim H

    9 Feb 2016 Pre-Release Testers Portland, OR USA

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

  15. 2 months ago

    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.

  16. Norman P

    May 14 Pre-Release Testers, Xojo Pro www.great-white-software.com/b...

    @David Z 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.

    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://docs.xojo.com/EncodeBase64

or Sign Up to reply!