-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWsClient.cpp
92 lines (79 loc) · 2.16 KB
/
WsClient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//#include "UserConfig.h"
#include "Wsclient.h"
// Skip HTTP headers so that we are at the beginning of the response's body
bool WsClient::skipResponseHeaders()
{
bool outcome = false;
while (client.connected())
{
String line = client.readStringUntil('\n');
// Serial.println("HEADERS: " + line);
if (line == "\r")
{
// Serial.println(F("headers received"));
outcome = true;
break;
}
}
return outcome;
}
// Open connection to the HTTP server
bool WsClient::httpsConnect(String hostName, String fingerprint)
{
// Serial.println("Free Heap: " + String(ESP.getFreeHeap()));
// Serial.println("Connecting to " + hostName);
bool outcome = client.connect(hostName.c_str(), 443);
// Serial.println(outcome ? F("Connected") : F("Connection Failed!"));
if (outcome)
{
if (fingerprint != "")
{
if (client.verify(fingerprint.c_str(), hostName.c_str()))
{
// Serial.println(F("certificate matches"));
}
else
{
// Serial.println(F("certificate doesn't match"));
outcome = false;
}
}
}
return outcome;
}
// Send the HTTP GET request to the server
bool WsClient::httpsGet(String host, String url)
{
client.print(F("GET "));
client.print(url);
client.println(F(" HTTP/1.1"));
client.print(F("Host: "));
client.println(host);
client.println(F("User-Agent: ESP8266"));
client.println(F("Content-Type: application/json; charset=UTF-8"));
client.println(F("Connection: close"));
client.println();
return true;
}
// Send the HTTP POST request to the server
bool WsClient::httpsPost(String host, String url, String postData)
{
client.print(F("POST "));
client.print(url);
client.println(F(" HTTP/1.1"));
client.print(F("Host: "));
client.println(host);
client.println(F("Connection: close"));
client.println(F("Content-Type: application/json"));
client.println(F("User-Agent: ESP8266"));
client.print(F("Content-Length: "));
client.println(postData.length());
client.println();
client.print(postData);
}
// Close the connection with the HTTP server
void WsClient::disconnect()
{
// Serial.println(F("Disconnect"));
client.stop();
}