Ethernet shield 400 Bad Request

I'm having a problem with my ethernet shield server connection. I connected my ethernet shield to a router ( client mode ) and then connect this router to my phone hotspot. This is my code:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

char server[] = "www.xxx";

IPAddress ip(192, 168, 1, 177);

EthernetClient client;

void setup() {

Serial.begin(9600);
while (!Serial) {
;
}

// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");

Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
Serial.println(Ethernet.localIP());

// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET http://xxx/ccc.php HTTP/1.1");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}

void loop() {

if (client.available()) {
char c = client.read();
Serial.print(c);
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();

// do nothing forevermore:
while (true);
}
}

here is what showing on the serial monitor :

connecting...
192.168.43.107 //
connected
HTTP/1.1 400 Bad Request
Date: Mon, 23 May 2016 17:45:44 GMT
Server: Apache
Content-Length: 226
Connection: close
Content-Type: text/html; charset=iso-8859-1

400 Bad Request

Bad Request

Your browser sent a request that this server could not understand.

disconnecting.

it worked when i directly connect it with my wifi router, but when trying to connect it to my hotspot using a client mode router it gives a Bad request. any help please ?

client.println("GET http://xxx/ccc.php HTTP/1.1");

The ethernet shield can only handle the http:// protocol, so that is NOT part of the request.

You've already defined the server to connect to. It doesn't make sense to connect to server one and try to run a script on another server, so the server is NOT part of the request.

thanks for the reply.the code worked fine when directly connecting the ethernet shield to my router. All i'm trying to do is to send http request to my server.
how can i change the code so that it can work on my hotspot using a client mode router. any thoughts? and why isn't it working on my hotspot but working fine when directly connecting it to my internet router?

but when trying to connect it to my hotspot using a client mode router

I have no idea what this means. The ethernet shield has a cable that needs to be connected to something. What, exactly, are you connecting it to?

the code worked fine when directly connecting the ethernet shield to my router.

It's better to be good than lucky.

i'm connecting it to a small router ( client mode ) via cable. it works as a bridge between the ethernet shield and my hotspot. if i connect my laptop to this router i'll have an internet access from my hotspot. but it's not working when i use it with my ethernet shield ( connect it via cable to my router).
However this same code works fine if i connect it directly to my home router via cable.

Use DHCP to get an IP from the client router. You shouldn't need to change anything else on the Arduino. The client mode router will do the rest. I use MikroTik routers set as clients, and this stuff works fine.

You must set up the client router correctly. Normally not for a beginner though.

The local ip of the ethernet shield is correct.
the small router ip is 192.168.43.1 and the local ip of the ethernet shield shows 192.168.43.203

but anything wrong in my code? is there any restrictions when bridging ? or is the hotspot the problem ?
as i said before it works fine when directly connecting it to my home router but using another router with a client mode and a hotspot isn't working for me.

This is all I can find, and it applies mainly if you use a domain name rather than an IP for the server.

// change this
if (client.connect(server, 80)) {
// to this
if (client.connect(server, 80) == 1) {

You must be more specific. "Isn't working" is too vague. How do you know it isn't working? What message do you get? If you bridged (or routed in my case) the router correctly, this should work. If you didn't, it won't.

it's giving HTTP/1.1 400 Bad Request. But i can't find out why, that's the problem.

connecting...
connected
HTTP/1.1 400 Bad Request
Date: Mon, 23 May 2016 19:00:56 GMT
Server: Apache
Content-Length: 226
Connection: close
Content-Type: text/html; charset=iso-8859-1

400 Bad Request

Bad Request

Your browser sent a request that this server could not understand.

disconnecting.

This is the request:

// Make a HTTP request:
client.println("GET /ccc.php HTTP/1.1");
client.println("Host: www.mydomain.com\r\nConnection: close\r\n");

I modified it a little to help you get past the 400 error. Replace www.mydomain.com with your domain name. I also combined a few lines into one to send all that in one packet. insure you leave all the "\r\n" stuff in the right place. One after the Host and one after the Connection.

edit: Insure you change the connect evaluation to the one I posted above. This:

// change this
if (client.connect(server, 80)) {
// to this
if (client.connect(server, 80) == 1) {

thanks man. it's working now!

now i'm trying to access the same php file but sending data with it. the php file is located in a folder named Z.

char server[] = "www.myserver.com";

======================

if (client.connect(server, 80) == 1) {

client.println("GET Z/ccc.php?data=82.112.162.256 HTTP/1.1");
client.println("Host: www.myserver.com\r\nConnection: close\r\n");

it's not working this way. what should i change?

i think i forgot to add / before Z. :smiley:
Thanks Again :slight_smile:

There you have it! :slight_smile: