Maclos
June 10, 2017, 7:46pm
1
Hi there.
I'm trying to send a http post to server, I got this code:
#include <ESP8266WiFi.h>
const char* ssid = "Putostodosmenosyo";
const char* password = "CetriX.2016%Gdl";
const char* host = "co2kelling.hopto.org";
const String Cadenadatos = "info=Micanton|2017-06-09%12:00|25" ;
//const String Tamano = String(Cadenadatos.length()) ;
const String postiu = "POST /wsCo2Keellingv1002/wsCo2Keelling.asmx/GuardaInfoArduino HTTP/1.1\r\nHost: 187.201.44.110:80\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length:144\r\n\r\n?info="+String(Cadenadatos)+"\r\n" ;
const String postam = String(postiu);
void setup() {
Serial.begin(9600);
delay(100);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(2000) ;
}
void loop() {
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host,httpPort)) {
Serial.println("Conexion fallida");
return;
}
//String data = "pst=temperature>" + String(random(0,100)) +"||humidity>" + String(random(0,100)) + "||data>text";
Serial.print("Requesting POST: ");
// Send request to the server:
//client.println("POST /wsCo2Keellingv1002/wsCo2Keelling.asmx/GuardaInfoArduino HTTP/1.1");
client.print("POST /wsCo2Keellingv1002/wsCo2Keelling.asmx/GuardaInfoArduino HTTP/1.1\r\n");
client.print("Host: 187.201.44.110\r\n");
client.print("Content-Type: application/x-www-form-urlencoded\r\n");
client.print("Content-Length: ");
client.print(postiu.length()+"\r\n");
client.println();
client.print(String(Cadenadatos));
delay(500); // Can be changed
if (client.connected()) {
client.stop(); // DISCONNECT FROM THE SERVER
}
Serial.println();
Serial.println("closing connection");
delay(5000);
}
I'm using the ESP8266 Library to send, I use the Arduino UNO to connect FTDI
On the monitor Serial I got responses like this:
Requesting POST:
closing connection
What am I doing wrong??
Since you sent a request, maybe you should get and display the response from the server. That might you troubleshoot your problem.
Maclos
June 10, 2017, 8:21pm
3
And how Can I get the responses?
I don't use a 8266, but it should be something like this.
Remove this.
delay(500); // Can be changed
if (client.connected()) {
client.stop(); // DISCONNECT FROM THE SERVER
}
Replace with this.
while (client.connected()) {
while (client.available()) {
Serial.write(client.read());
}
}
client.stop();
Maclos
June 10, 2017, 9:58pm
5
Hey SurferTim, I've obtained the response!!
But I guess isn't goot, because obtain this:
Requesting POST: HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Sat, 10 Jun 2017 21:56:48 GMT
Connection: close
Content-Length: 311
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request</h2>
<hr><p>HTTP Error 400. The request is badly formed.</p>
</BODY></HTML>
Is it somekind of issue on my request?
This doesn't look right. It is the page request.
/wsCo2Keellingv1002/wsCo2Keelling.asmx/GuardaInfoArduino
Maclos
June 10, 2017, 10:52pm
7
This looks like a document, not a directory.
wsCo2Keelling.asmx
edit: But I don't think that is it. It may be you didn't url encode the data.
this is the original:
info=Micanton|2017-06-09%12:00|25
This is url encoded:
info%3DMicanton%7C2017-06-09%2512%3A00%7C25
Maclos
June 11, 2017, 12:09am
9
This is my request format:
POST /wsCo2Keellingv1002/wsCo2Keelling.asmx/GuardaInfoArduino HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length
info=string
On the line "info=string"
I put the parameters, So need I put
like this?:
info=info%3DMicanton%7C2017-06-09%2512%3A00%7C25
Sorry this is new for me.
This request gets a 200 response, but the server complains about the date/time. That is something in the Cadenadatos variable.
client.print("POST /wsCo2Keellingv1002/wsCo2Keelling.asmx/GuardaInfoArduino HTTP/1.1\r\n");
client.print("Host: co2kelling.hopto.org\r\n");
client.print("Content-Type: application/x-www-form-urlencoded\r\n");
client.print("Content-Length: ");
client.println(Cadenadatos.length());
client.println();
client.print(Cadenadatos);
Maclos
June 11, 2017, 7:04pm
11
Thanks for the answer SurferTim, my problem was solver thanks at you.
SurferTim:
This request gets a 200 response, but the server complains about the date/time. That is something in the Cadenadatos variable.
client.print("POST /wsCo2Keellingv1002/wsCo2Keelling.asmx/GuardaInfoArduino HTTP/1.1\r\n");
client.print("Host: co2kelling.hopto.org \r\n");
client.print("Content-Type: application/x-www-form-urlencoded\r\n");
client.print("Content-Length: ");
client.println(Cadenadatos.length());
client.println();
client.print(Cadenadatos);
You might also look at your use of println(). You are manually using print("\r\n"), then using println(). Println() only send "\n".
The following is Stolen from StackOverFlow in message HTTP Header line break style
RFC2616 states at the beginning of Section 2.2
CR = <US-ASCII CR, carriage return (13)>
LF = <US-ASCII LF, linefeed (10)>
HTTP/1.1 defines the sequence CR LF as the end-of-line marker for all protocol elements except the entity-body
I know it is anal, but 'The Standard' wants "/r/n" it does not require acceptance of just "/n" in headers. So just do it correctly, it will remove one possible failure mode.
Chuck.