http connection refused error with esp8266

hello everyone

i have been working on esp8266 for a week now.

i am getting an http connection refused error for this set of code.

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#define USE_SERIAL Serial
int id,temp_id=0,i=0;
ESP8266WiFiMulti wifiMulti;
void setup() {
      
    USE_SERIAL.begin(115200);
    delay(100);
    WiFi.mode(WIFI_STA);
    wifiMulti.addAP("DIGISOL", "edot2018");

    while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
    delay(250);
    Serial.print(".");
  }

}

void loop() {
        
        HTTPClient http,http2;
        http.begin("http://192.168.1.12/FlightStats/api/Flight/GetArrivingFlightsStatus/"); //HTTP
        delay(250);
        http2.begin("http://192.168.1.12/FlightStats/api/Flight/GetDepartingFlightsStatus/");
        delay(250);
        if(http.begin("http://192.168.1.12/FlightStats/api/Flight/GetArrivingFlightsStatus/") == false) {
           USE_SERIAL.println("http.begin() failed");
          exit(0);
        }   
        //start connection and send HTTP header
        int httpCode = http.GET();
        int httpCode2 = http2.GET();
        // [HTTP] GET... failed, error: connection refused
       if(httpCode > 0 && httpCode2 > 0) {
            // HTTP header has been send and Server response header has been handled
            // file found at server
            if(httpCode == HTTP_CODE_OK && httpCode2 == HTTP_CODE_OK) {
                String myString = http.getString();
                String myString2 = http2.getString();

                const size_t bufferSize = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(8) + 370;
                 DynamicJsonBuffer jsonBuffer(bufferSize);
                 JsonObject& root = jsonBuffer.parseObject(myString);
                 const char* descpription = root["Description"];
                 JsonObject& root1 = jsonBuffer.parseObject(myString2);
                 const char* descpription2 = root1["Description"];
                  if(i=0){
                 USE_SERIAL.println(descpription);
                 USE_SERIAL.println(descpription2); 
                 i=1;
                  }
            }
        } 
        else {
            USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }
        http.end();
}

help plz

Have you tried to manually do those steps to make sure they work before try to get the ESP8266 --- which module? --- to do it?

Have you tried to manually do those steps

i can access this link on the browser.

with that code i am able to get the correct result but sometime it gives connection refused error,or sometime it gives nothing.

that is the issue.

i just want to know whether my code is correct or not, or i need to make any changes?

Could it be a timing issue?

Do you ever get connection refused if you are using the browser?

Anyway, someone wrote that they have good code. ESP8266 occasionally drops data - Networking, Protocols, and Devices - Arduino Forum

This worked for me. I had changed the http.begin() to use my endpoint. I didn't add the SSL fingerprint though.

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");

I found my fingerprint by adding the full domain at this website.

GRC | SSL TLS HTTPS Web Server Certificate Fingerprints  . Just enter this in the second argument for the http.begin() method like

http.begin("https://yahoo.com","DE:9E:8E:5B:C0:D9:AD:A9:E8:C4:68:F4:76:DD:57:75:12:90:EB:D6").

this work for me, i try client for default

#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>

WiFiMulti WiFiMulti;

void setup() {

Serial.begin(115200);
// Serial.setDebugOutput(true);

Serial.println();
Serial.println();
Serial.println();

WiFi.mode(WIFI_STA);
WiFiMulti.addAP(“wifi”, “password”);

// wait for WiFi connection
Serial.print(“Waiting for WiFi to connect…”);
while ((WiFiMulti.run() != WL_CONNECTED)) {
Serial.print(".");
}
Serial.println(" connected");
}

void loop() {
WiFiClientSecure *client = new WiFiClientSecure;
HTTPClient https;

Serial.print("[HTTPS] begin…\n");
if (https.begin(*client, “https://hostname.com”)) { // HTTPS
Serial.print("[HTTPS] GET…\n");
int httpCode = https.GET();
if (httpCode > 0) {
Serial.printf("[HTTPS] GET… code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = https.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTPS] GET… failed, error: %s\n", https.errorToString(httpCode).c_str());
}

https.end();
} else {
Serial.printf("[HTTPS] Unable to connect\n");
}

delete client;

Serial.println();
Serial.println(“Waiting 10s before the next round…”);
delay(10000);
}