mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-22 21:23:07 +03:00
add examples/BasicHttpClient/BasicHttpClient.ino
fix get size only fingerprint when strlen > 0
This commit is contained in:
parent
5e1a65609c
commit
1e7b9688a5
@ -0,0 +1,77 @@
|
||||
/**
|
||||
* BasicHttpClient.ino
|
||||
*
|
||||
* Created on: 24.05.2015
|
||||
*
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266WiFiMulti.h>
|
||||
|
||||
#include <ESP8266httpClient.h>
|
||||
|
||||
|
||||
ESP8266WiFiMulti WiFiMulti;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
|
||||
|
||||
for(uint8_t t = 4; t > 0; t--) {
|
||||
Serial.printf("[SETUP] WAIT %d...\n", t);
|
||||
Serial.flush();
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
WiFiMulti.addAP("SSID", "PASSWORD");
|
||||
|
||||
//WiFi.disconnect();
|
||||
while(WiFiMulti.run() != WL_CONNECTED) {
|
||||
delay(100);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if((WiFiMulti.run() == WL_CONNECTED)) {
|
||||
httpClient http;
|
||||
|
||||
Serial.print("[HTTP] begin...\n");
|
||||
|
||||
http.begin("192.168.1.12", 80, "/test.html");
|
||||
|
||||
Serial.print("[HTTP] GET...\n");
|
||||
if(http.GET()) {
|
||||
|
||||
Serial.print("[HTTP] GET... ok.\n");
|
||||
|
||||
size_t len = http.getSize();
|
||||
|
||||
uint8_t buff[128] = { 0 };
|
||||
WiFiClient stream = http.getStream();
|
||||
while(http.connected() && len > 0) {
|
||||
size_t size = stream.available();
|
||||
int c = stream.readBytes(buff, ((size > 128) ? 128 : size));
|
||||
|
||||
Serial.write(buff, c);
|
||||
len -= c;
|
||||
|
||||
delay(0);
|
||||
}
|
||||
Serial.println();
|
||||
Serial.print("[HTTP] connection closed or file end.\n");
|
||||
|
||||
} else {
|
||||
|
||||
Serial.print("[HTTP] GET... fail.\n");
|
||||
}
|
||||
|
||||
delay(10000);
|
||||
}
|
||||
}
|
@ -36,7 +36,7 @@ httpClient::httpClient() {
|
||||
_currentHeaders = NULL;
|
||||
|
||||
_returnCode = 0;
|
||||
_size = 0;
|
||||
_size = -1;
|
||||
}
|
||||
|
||||
httpClient::~httpClient() {
|
||||
@ -51,11 +51,18 @@ httpClient::~httpClient() {
|
||||
}
|
||||
|
||||
void httpClient::begin(const char *host, uint16_t port, const char * url, bool https, const char * httpsFingerprint) {
|
||||
|
||||
_host = host;
|
||||
_port = port;
|
||||
_url = url;
|
||||
_https = https;
|
||||
_httpsFingerprint = httpsFingerprint;
|
||||
|
||||
_returnCode = 0;
|
||||
_size = -1;
|
||||
|
||||
_Headers = "";
|
||||
|
||||
}
|
||||
|
||||
void httpClient::begin(String host, uint16_t port, String url, bool https, String httpsFingerprint) {
|
||||
@ -121,6 +128,15 @@ int httpClient::POST(String payload) {
|
||||
return POST((uint8_t *) payload.c_str(), payload.length());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* size of message body / payload
|
||||
* @return -1 if no info or > 0 when Content-Length is set by server
|
||||
*/
|
||||
int httpClient::getSize(void) {
|
||||
return _size;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the stram of the tcp connection
|
||||
* @return WiFiClient
|
||||
@ -194,13 +210,7 @@ bool httpClient::hasHeader(const char* name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* size of message body / payload
|
||||
* @return 0 if no info or > 0 when Content-Length is set by server
|
||||
*/
|
||||
size_t httpClient::getSize(void) {
|
||||
return _size;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* init TCP connection and handle ssl verify if needed
|
||||
@ -209,7 +219,7 @@ size_t httpClient::getSize(void) {
|
||||
bool httpClient::connect(void) {
|
||||
|
||||
if(connected()) {
|
||||
DEBUG_HTTPCLIENT("[HTTP-Client] connect. already connected!\n");
|
||||
DEBUG_HTTPCLIENT("[HTTP-Client] connect. already connected, reuse!\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -229,7 +239,7 @@ bool httpClient::connect(void) {
|
||||
|
||||
DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u.\n", _host.c_str(), _port);
|
||||
|
||||
if(_https) {
|
||||
if(_https && _httpsFingerprint.length() > 0) {
|
||||
if(_tcps->verify(_httpsFingerprint.c_str(), _host.c_str())) {
|
||||
DEBUG_HTTPCLIENT("[HTTP-Client] https certificate matches\n");
|
||||
} else {
|
||||
|
@ -59,7 +59,7 @@ class httpClient {
|
||||
bool hasHeader(const char* name); // check if header exists
|
||||
|
||||
|
||||
size_t getSize(void);
|
||||
int getSize(void);
|
||||
|
||||
WiFiClient & getStream(void);
|
||||
|
||||
@ -89,7 +89,7 @@ class httpClient {
|
||||
size_t _headerKeysCount;
|
||||
|
||||
int _returnCode;
|
||||
size_t _size;
|
||||
int _size;
|
||||
|
||||
|
||||
bool connect(void);
|
||||
|
Loading…
x
Reference in New Issue
Block a user