Hello all,
There is a problem I have been stuck with for days so any advices from you guys will be such a great support.
I am writing the code to connect to IIS server and making HTTPS request to retrieve and post data onto the server by esp32( I am using Wificlientsecure library but the base of it is also written with mbedtls library so I believe there should be the same the way they make the connection to server). Right now, I have successfully established connection with the server with TLS handshake. However, when sending a GET or POST request to the server I get the error “Connection reset by peer” and the log on server also return status code 0. I tried to connect with the server from web browser and it just works well with no issues or errors popped up. So I think there would be something to deal with my code rather than from the server side. Below is my code for reference:
#include <WiFiClientSecure.h>
#include <string.h>
// You can use x.509 client certificates if you want
//const char* test_client_key = “”; //to verify the client
//const char* test_client_cert = “”; //to verify the client
WiFiClientSecure client;
String postData;
String DataToSend;
int SentLen = 0;
int PostLen = 0;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
delay(100);
postData = “Shift=1&UserID=1129&JobNumber=F0000001&Reason=CNC DEFECTIVE”;
SentLen = postData.length();
DataToSend =
"GET /Program_http/ HTTP/1.1\r\nConnection: close\r\nHost: " + \
String( server ) + “\r\n” +
“User-Agent: Esp32Testing\r\n\r\n\0”;
// “Content-Type: application/x-www-form-urlencoded\r\n” +
"Content-Length: " + String(SentLen ) + “\r\n” +
postData;
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
Serial.print(“.”);
// wait 1 second for re-trying
delay(1000);
}
Serial.print("Connected to ");
Serial.println(ssid);
client.setCACert(test_root_ca);
//client.setCertificate(test_client_cert); // for client verification
//client.setPrivateKey(test_client_key); // for client verification
Serial.println(“\nStarting connection to server…”);
if ( client.connect( server, 443 ) != 1 ){
Serial.println(“Connection failed!” );
Serial.println( strerror( errno ) );
} else {
Serial.println(“Connected to server!” );
Serial.println( String( errno ) );
// Make a HTTP request
char p; //To read/write data from server;
int p2;
const char *Data = DataToSend.c_str();
while( *Data != '\0' ){
p = *Data;
client.write( p );
Serial.print( p );
Data++;
}
static unsigned long st = millis();
while( client.connected() ){
p = client.read();
p2 = (int) p;
Serial.print( p2 );
if( ( p == '\r' ) | ( ( p2 == 255 ) & ( millis() - st >= 4000 ) ) ) {
st = millis();
break;
}
}
// if there are incoming bytes available
// from the server, read them and print them
while ( client.available() ) {
char c = client.read();
Serial.print( c );
}
Serial.println();
}
Serial.println( “Process complete” );
Serial.println( strerror( errno ) );
client.stop();
}
void loop() {
// do nothing
}
I hid the wifi’s passwd and the certificate for some safety issues. Please understand. Here right after connect function successfully connects to the server. I sent the request and tried to read the data from server but failed. So any one can give me some advices how to fix this issue should by a great support. This is almost the end of my project and only this more thing holds me back from completing this project.
Tks.