Pages: [1]   Go Down
Author Topic: Remote connection getting closed - hitting an arbitary URL?  (Read 1575 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I'm trying to put together a project that on certain events will hit a URL with some parameters that will set various commands on a WebCam. 

A made up example would be something like:  http://<webcam ip address>/camera_control.cgi?user=user&password=password&command=<command>

This works fine in a browser, but I'm having a little trouble getting this to work with the ethernet shield.

The program connects fine and the following is output:

HTTP/1.1 401 Unauthorized
Server: Netwave IP Camera
Date: Wed, 01 Feb 2012 21:37:37 GMT
WWW-Authenticate: Basic realm="ipcamera_000DC5D34ADE"
Content-Type: text/html
Content-Length: 140
Connection: close

<HTML><HEAD><TITLE>401 Unauthorized</TITLE></HEAD>
<BODY BGCOLOR="#cc9999"><H4>401 Unauthorized</H4>
Authorization required.
</BODY></HTML>

after which the connection is closed (from the WebCam side I'd imagine), which means I'm unable to do the next step and execute the actual command.

Note that I'm able to connect to google etc just fine and execute commands.

I guess the WebCam is prevent the type of connection that the Arduino board is attempting (its not an HTTPS connection) - is there an alternative way to hit arbitrary URLs without first making a connection?

As a temporary workaround I'm going to setup a script on a server that the Ethernet board can connect to, which will in turn trigger the webcam command.

Any ideas would be much appreciated!

thanks
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like the web server is expecting a username and password.  I'm guessing that you entered that data in your browser the first time you connected to the camera and your browser remembers your login information.  The URL syntax is:

http://<username>:<password>@<webcam ip address>/camera_control.cgi?user=user&password=password&command=<command>
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi John,

Thanks for your response.

The camera does indeed require basic authentication, which I can workaround via the browser as you say, but the problem I have is that I cant get that far as the process fails shortly after the following code:

Code:
if(client.connect(camera, 80)>0) {

It connects, but then outputs the 401 error above.  I agree that it expects authentication, I'm just at a loss on how to provide it via Arduino !

thanks
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Send all after the host (<webcam ip address>) in the GET part of the header.
Code:
client.write("GET /camera_control.cgi?user=user&password=password&command=<command> HTTP/1.0\r\n\r\n");
Insure there are no illegal characters (like spaces) in any of that. Otherwise, you will need to url encode it.


Logged

0
Offline Offline
Shannon Member
****
Karma: 221
Posts: 12710
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to send BasicAuth header(s) - check out the relevant RFC on HTTP protocol.
Logged

[ I won't respond to messages, use the forum please ]

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, MarkT. I missed that. That is for the user:password sent in the server? Like
http://user:password@server.com

If so, I will remember to add that to my responses.  smiley
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your authentication should look something like this:
Code:
client.write(
"GET /camera_control.cgi?user=user&password=password&command=<command> HTTP/1.0\r\n"
"Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==\r\n"
"\r\n");

You can use this website to encode your USERNAME:PASSWORD in base64:

        http://www.opinionatedgeek.com/dotnet/tools/base64encode/

For this example, "Aladdin:open sesame" has been encoded as "QWxhZGRpbjpvcGVuIHNlc2FtZQ==".
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks very much everyone - what a great forum.

The problem I was actually having was that the connection would be made using

Code:
client.connect(camera, 80)

but when I attempted to control the camera (using the various authentication methods mentioned - the base64 method was something I'd tried in java and PHP, but not here - very useful, thanks!) it failed as the connection had already been closed.

The posts above however inspired me to try a different approach: instead of opening the connection and keeping it open, I've tried opening the connection on a given event, controlling the camera and then stopping  the connection, as follows:

Code:
if(<some event>) {
  if(client.connect(camera, 80)>0) {
    client.write("GET /camera_control.cgi?param=5&value=1&user=<user>&pwd=<password> HTTP/1.0\r\n"
                     "Authorization: Basic [base64 encoding of username:password]==\r\n"
                     "\r\n");    
    client.stop();
  }
}

This works a treat - I guess due to either the cameras implemenation or Arduinos connections are terminated if not authenticated in a reasonable time window.

Thanks all for your useful advice!
Logged

0
Offline Offline
Tesla Member
***
Karma: 147
Posts: 9750
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting, I've got an IP cam in a box that I could tinker with. Might also be of use with the FosCam pan/tilt setup.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: