getRequest to a server

hello,

I am trying to solve how to make a getRequest to a server using only the http ie:

I ask: "http://www.myserver.com/?mode=1&user=username&pass=sometext"
server send: "1"

I need to save the resuld received into a variable like in Processing ie:

String answer = something("http://www.myserver.com/?mode=1&user=username&pass=sometext");

I am not able to find any solution. Can someone help?

Thenk you,
Petr

I know this code. Unfotunately it is not the code I hope to get.

I did this:

...
byte server[] = { 87, 156, 219, 170 }; => this is set as IP for myserver.com
...
client.println("http://myserver.com/data/DEMO_MODE/system/phpCommunication/?mode=5&user=test&pass=test");
...

char c = client.read();
...

but the server don't send "1" nor "0", but answers with a whole page it doesn't understands the request. The request works using the ordinary browser. What do you think?

but the server don't send "1" nor "0", but answers with a whole page it doesn't understands the request. The request works using the ordinary browser. What do you think?

I think you need to show the output that you get from the server, as well as the PHP script that is being accessed.

just click on the link http://myserver.com/data/DEMO_MODE/system/phpCommunication/?mode=5&user=test&pass=test [/url] and you get what I need to read. Nothing more is sent as an answer...

hubajs:
...
client.println("http://soupis.cz/data/DEMO_MODE/system/phpCommunication/?mode=5&user=test&pass=test");
...

^^ This is wrong. If you sent just this line to your server, no wonder it replies with anger :slight_smile: You should take a look on some "How HTTP works" tutorial. Try this instead:

client.print("GET /data/DEMO_MODE/system/phpCommunication/?mode=5&user=test&pass=test HTTP/1.1\n\r");
client.print("Host: soupis.cz\n\r");
client.print("Connection: close\n\r");
client.print("\n\r");

That's valid HTTP/1.1 request. You might even find simple HTTP/1.0 request working:

client.print("GET /data/DEMO_MODE/system/phpCommunication/?mode=5&user=test&pass=test HTTP/1.0\n\r\n\r");

Thank you friends for your sudgestions. Unfortunately I am missing something, because I get answer with following header:

[size=6pt]HTTP/1.1 301 Moved Permanently
Date: Tue, 07 Jun 2011 18:54:00 GMT
Server: Apache
Location: http://parkovani/?mode=5&user=test&pass=test
Cache-Control: max-age=7200
Expires: Tue, 07 Jun 2011 20:54:00 GMT
Content-Length: 319
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://parkovani/?mode=5&amp;user=test&amp;pass=test">here</a>.</p>
<hr>
<address>Apache Server at parkovani Port 80</address>
</body></html>[/size]

Here is the code:

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,178 };
byte server[] = { 88, 146, 119, 160 }; // soupis.cz
Client client(server, 80);

void setup() {
  Ethernet.begin(mac, ip);
  Serial.begin(19200);
  if (client.connect()) {
    Serial.println("connected");
    client.print("GET /data/DEMO_MODE/system/phpCommunication/?mode=5&user=test&pass=test HTTP/1.0\n\r\n\r");
  }
  else {     Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {     
    char c = client.read();     
    Serial.print(c);
  }
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

So, what happens if you use:

client.print("GET /?mode=5&user=test&pass=test HTTP/1.0\n\r\n\r");

Your server is not handling an HTTP 1.0 request. You will need to formulate a 1.1 request as happz suggested.

PaulS: what happens with directories? ... it is not working

maniacbug: I formulated the request, but have got the answer herebellow. Mabye shal I use another HTML than 1.0 or 1.1 ? How to ask server what does it accepts?

HTTP/1.1 400 Bad Request
Date: Tue, 07 Jun 2011 20:29:48 GMT
Server: Apache
Content-Length: 285
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.

</p>
<hr>
<address>Apache Server at parkovani Port 80</address>
</body></html>

Post the code that generated this please.

of course...

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,178 };
byte server[] = { 88, 146, 119, 160 }; // soupis.cz
Client client(server, 80);

void setup() {
  Ethernet.begin(mac, ip);
  Serial.begin(19200);
  delay(500);
  Serial.println("connecting...");
  if (client.connect()) {
    Serial.println("connected");
    client.print("GET /data/DEMO_MODE/system/phpCommunication/?mode=5&user=test&pass=test HTTP/1.1\n\r");
    client.print("Host: soupis.cz\n\r");
    client.print("Connection: close\n\r");
    client.print("\n\r");
  }
  else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

Strange. Well, there is nothing wrong with your HTTP. I just opened the port by hand and tried it, and got this

HTTP/1.1 200 OK
Date: Tue, 07 Jun 2011 22:26:00 GMT
Server: Apache
Cache-Control: max-age=7200
Expires: Wed, 08 Jun 2011 00:26:00 GMT
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

1
1
0

Must be something in usage of Ethernet library.

OK, it is good news it is working on your arduino. Now the prblm is on my SW/HW side then.

Can you please confirm/sudgest:
I use arduino 22 and library inside, ethernet shield, I connect to router

Thanks, Peter

hubajs:
of course...

#include <SPI.h>

#include <Ethernet.h>
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,178 };
byte server[] = { 88, 146, 119, 160 }; // soupis.cz
Client client(server, 80);

void setup() {
  Ethernet.begin(mac, ip);
  Serial.begin(19200);
  delay(500);
  Serial.println("connecting...");
  if (client.connect()) {
    Serial.println("connected");
    client.print("GET /data/DEMO_MODE/system/phpCommunication/?mode=5&user=test&pass=test HTTP/1.1\n\r");
    client.print("Host: soupis.cz\n\r");
    client.print("Connection: close\n\r");
    client.print("\n\r");
  }
  else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;:wink:
      ;
  }
}

Seems correct, just one fix (and I admit it's my fault) - each HTTP line has to end with pair, so "\r\n", not "\n\r" as I wrote... Sorry for that, with this fix it should work, I just checked with your server manually.

    client.print("GET /data/DEMO_MODE/system/phpCommunication/?mode=5&user=test&pass=test HTTP/1.1\r\n");
    client.print("Host: soupis.cz\r\n");
    client.print("Connection: close\r\n");
    client.print("\r\n");

Thanks, the code works now. Thank you!