I took the Webclient (repeating) example. I want to make dynamic requests to my web server.
But I fail to construct the right "String"(?).. See my code below. Actuall the function shall be called with two integer variables and it shall do a request to the server.
In my code are already 3 test cases, from which only one works (1).
- With case 1, I "hardcode" the string and send it to the server
- With case 2, I use a String variable holding exactly(?) the same data => fails
- With case 3, Here I just split the (working) string (see case1) on two lines => also fails
Hardcoding the request (case 1) works like a charm. Any other method I tried fails and I do not know why??? :~
The reply that I get from the web server is:
HTTP/1.0 400 Bad Request
Content-Type: text/html
Connection: close
<title>Bad Request</title>Only HTTP 1.x supported
And here is my code (of course I enable only one of the cases at a time!!:
void xs1Request(int actor, int funct) {
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
//--- This is what I actually want to do, but it failed (like with cmd3)
String cmd0 = "GET /control?callback=cname&cmd=set_state_actuator&number=";
String cmd1 = cmd0 + 1 +"&function="+2+" HTTP/1.1";
// Sendind cmd1 failed, so I simplified to this for testing (case 3)
String cmd3 = "GET /control?callback=cname&cmd=set_state_actuator&number=1&function=2 HTTP/1.1";
// case 1 works:
client.println("GET /control?callback=cname&cmd=set_state_actuator&number=1&function=2 HTTP/1.1");
// case 2 does not work (see reply above):
// client.println(cmd3);
// case 3 does not work too (damn, is the same as case 1, or is it not???):
// client.print("GET /control?callback=cname&cmd=set_state_actuator&number=");
// client.println("1&function=2 HTTP/1.1");
client.println("Host: www.arduino.cc");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println("disconnecting.");
client.stop();
}
}