PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : HTTP GET request mit ServerCertificateValidation & SecurityProtocol verwenden



TBMSam
05.11.2020, 13:12
Hallo zusammen,

mit Hilfe von https://randomnerdtutorials.com/esp8266-nodemcu-http-get-post-arduino/ habe ich einen Arduino-Sketch erstellt, der einen HTTP GET request an einen WebService sendet, welcher derzeit auf einem Server läuft:



#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

const char* ssid = "MyWiFiName";
const char* password = "MyWifiPassword";

//My Domain name with URL path or IP address with path
String serverName = "http://192.168.200.123:55558/api/DBGeraetestatus";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
//unsigned long timerDelay = 5000;

void setup() {
Serial.begin(115200);

WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());

Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
//Send an HTTP POST request every 10 minutes
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;

String serverPath = serverName + "?filter=ipadresse&value=192.168.200.244";

// My Domain name with URL path or IP address with path
http.begin(serverPath.c_str());

// Send HTTP GET request
int httpResponseCode = http.GET();

if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}


Als Antwort kommt ein JSON mit einem 4-stelligen PIN rüber.

Der WebService auf dem Server sendet seinerseits einen weiteren HTTP GET request an eine spezielle Maschine, fügt aber zusätzlich noch NetworkCredentials (Name+Passwort) an, setzt den ServerCertificateValidationCallback und legt das SecurityProtocol fest, das von den vom ServicePointManager-Objekt verwalteten ServicePoint-Objekten verwendet werden soll:



private string getPinFromWebservice(string ip)
{
Uri.TryCreate(string.Format(Constants.generatePinP ath, ip), UriKind.Absolute, out requestUri);

var request = HttpWebRequest.Create(requestUri);
request.ContentType = "application/json";
request.Method = "GET";

NetworkCredential nc = new NetworkCredential("MyNetworkCredentialName", "MyNetworkCredentialPassword");
CredentialCache cache = new CredentialCache();
cache.Add(requestUri, "Basic", nc);
cache.Add(requestUri, "NTLM", nc);
request.Credentials = cache;

ServicePointManager.ServerCertificateValidationCal lback = delegate { return true; };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
var json = reader.ReadToEnd();
return System.Text.RegularExpressions.Regex.Replace(json, @"[^0-9]", "").ToString();
}
}
}


Durch Recherche habe ich herausgefunden, dass


http.setAuthorization(user, password);

Basic-Authentifikation hinzufügt und der HttpClient theoretisch https handeln kann, aber


begin();

dafür eventuell ein Zertifikat als Parameter benötigen könnte. (Quelle: https://github.com/esp8266/Arduino/blob/5d2563eee98ae116dc3d4b6bc14225efb3c85098/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h#L153).

Ist es möglich, den ServerCertificateValidationCallback und das SecurityProtocol in Arduino/C ebenfalls zu setzen? Also kann man den unteren C#-Code irgendwie übersetzen, sodass er auf meinem ESP8266 läuft und die HTTP GET requests mit https und angefügten NetworkCredentials ausführt? Also dass man den Server theoretisch überspringen und die requests vom Arduino direkt an die Maschine (und nicht an/über den Server) senden kann?

Freue mich über jede Antwort. Vielen Dank im Vorraus für sämtliche Hilfe.

Liebe Grüße