1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

rework HTTPclient examples

This commit is contained in:
Markus Sattler 2015-12-09 12:56:49 +01:00
parent e15c745e6f
commit 1dabac60a9
3 changed files with 22 additions and 10 deletions

View File

@ -43,24 +43,28 @@ void loop() {
USE_SERIAL.print("[HTTP] begin...\n"); USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url // configure traged server and url
//http.begin("192.168.1.12", 443, "/test.html", true, "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS //http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
http.begin("192.168.1.12", 80, "/test.html"); //HTTP http.begin("http://192.168.1.12/test.html"); //HTTP
USE_SERIAL.print("[HTTP] GET...\n"); USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header // start connection and send HTTP header
int httpCode = http.GET(); int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode) { if(httpCode) {
// HTTP header has been send and Server response header has been handled // HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server // file found at server
if(httpCode == 200) { if(httpCode == HTTP_CODE_OK) {
String payload = http.getString(); String payload = http.getString();
USE_SERIAL.println(payload); USE_SERIAL.println(payload);
} }
} else { } else {
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n"); USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
} }
http.end();
} }
delay(10000); delay(10000);

View File

@ -36,26 +36,31 @@ void setup() {
WiFiMulti.addAP("SSID", "PASSWORD"); WiFiMulti.addAP("SSID", "PASSWORD");
// allow reuse (if server supports it)
http.setReuse(true);
} }
void loop() { void loop() {
// wait for WiFi connection // wait for WiFi connection
if((WiFiMulti.run() == WL_CONNECTED)) { if((WiFiMulti.run() == WL_CONNECTED)) {
http.begin("192.168.1.12", 80, "/test.html"); http.begin("http://192.168.1.12/test.html");
//http.begin("192.168.1.12", 80, "/test.html");
int httpCode = http.GET(); int httpCode = http.GET();
if(httpCode) { if(httpCode) {
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server // file found at server
if(httpCode == 200) { if(httpCode == HTTP_CODE_OK) {
http.writeToStream(&USE_SERIAL); http.writeToStream(&USE_SERIAL);
} }
} else { } else {
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n"); USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
} }
http.end();
} }
delay(1000); delay(1000);

View File

@ -42,19 +42,20 @@ void loop() {
HTTPClient http; HTTPClient http;
USE_SERIAL.print("[HTTP] begin...\n"); USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url
http.begin("192.168.1.12", 80, "/test.html"); // configure server and url
http.begin("http://192.168.1.12/test.html");
//http.begin("192.168.1.12", 80, "/test.html");
USE_SERIAL.print("[HTTP] GET...\n"); USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header // start connection and send HTTP header
int httpCode = http.GET(); int httpCode = http.GET();
if(httpCode) { if(httpCode) {
// HTTP header has been send and Server response header has been handled // HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server // file found at server
if(httpCode == 200) { if(httpCode == HTTP_CODE_OK) {
// get lenght of document (is -1 when Server sends no Content-Length header) // get lenght of document (is -1 when Server sends no Content-Length header)
int len = http.getSize(); int len = http.getSize();
@ -91,6 +92,8 @@ void loop() {
} else { } else {
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n"); USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
} }
http.end();
} }
delay(10000); delay(10000);