Client.print with esp8266. not connecting to client

I am trying to make a pinger with my esp8266. The pinging itself is working perfectly. The only thing left to do is to send use the URL with the help of client.print.

This is the code I use:

client.print(String("GET ") + url2 + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");

When I manually put in "url2'' in the browser of my pc. It works. For some reason this is not working on the esp8266. Also the esp is connected to the WiFi and this seems to work fine too.

Could someone help me figure out what is wrong here?
Big thanks!

My guess is that there is something wrong with the parts of the sketch you did not show. It would also help to know what happens when "It works." in the browser and what happens when it "is not working on the esp8266"

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

// SETTINGS
const char* ssid          = "SSID";                                                                     // WiFi SSID
const char* password      = "MyPassword";                                                      // WiFi Password
const char* remote_host[] = {"192.168.2.21","192.168.2.15"};                                           // Hosts to check
const char* host          = "graph-eu01-euwest1.api.smartthings.com";                                  // Home automation API URL
String url                = "secondPartOfUrl";       // Home automation API URL suffix
String accToken           = "MyAccesToken";                                    // Home automation API Access Token

int delaySeconds          = 10;                                       // Number of seconds for next loop after checking all hosts
bool advancedDebug        = false;                                    // When true this will take longer as it waits for a response
bool heartBeat            = false;                                    // When true this will send current state with every loop

// DO NOT EDIT BELOW
const int httpsPort = 443;
int hostCount = sizeof(remote_host)/sizeof(char *); //array size
char* activeClients[sizeof(remote_host)];

// Get disconnecct event
WiFiEventHandler disconnectedEventHandler;

//+=============================================================================
// Gets called when device loses connection to the accesspoint
//
void lostWifiCallback (const WiFiEventStationModeDisconnected& evt) {
  Serial.println("Lost Wifi");
  // reset and try again, or maybe put it to deep sleep
  ESP.reset();
  delay(1000);
}

void setup() {
  Serial.begin(9600);
  delay(10);
  pinMode(LED_BUILTIN, OUTPUT);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println("Connecting to WiFi");
  disconnectedEventHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event)
  {
    Serial.println("Lost Wifi");
    ESP.reset();
    delay(1000);
  });
  
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(LED_BUILTIN, LOW);
    Serial.print(".");
    delay(250);
    Serial.print(".");
    digitalWrite(LED_BUILTIN, HIGH);
    delay(250);
  }
  Serial.println();
  Serial.print("WiFi connected with ip ");  
  Serial.println(WiFi.localIP());
}

void loop() {
  // Loop through hosts and check state
  for (int thisHost = 0; thisHost < hostCount; thisHost++) {
    
    Serial.print("[Pinging Host] ");
    Serial.print(remote_host[thisHost]);
  
    if(Ping.ping(remote_host[thisHost], 1)) {
      if(activeClients[thisHost] == "online"){
        if(heartBeat){
          if(sendToST(remote_host[thisHost], "online")){
            activeClients[thisHost] = "online";
          }
        }
        Serial.println(" -> ALREADY ONLINE");
      }else{
        Serial.println(" -> ONLINE");
        if(sendToST(remote_host[thisHost], "online")){
          activeClients[thisHost] = "online";
        }
      }
    } else {
      if(activeClients[thisHost] == "offline"){
        if(heartBeat){
          if(sendToST(remote_host[thisHost], "offline")){
            activeClients[thisHost] = "online";
          }
        }
        Serial.println(" -> ALREADY OFFLINE");
      }else{
        Serial.println(" -> OFFLINE");
        if(sendToST(remote_host[thisHost], "offline")){
          activeClients[thisHost] = "offline";
        }
      }
    }
  }
  Serial.print("Wait ");
  Serial.print(delaySeconds);
  Serial.println(" seconds...");
  delay(delaySeconds * 1000); 
}

bool sendToST(const char* pingHost, String state){
    
  // Use WiFiClientSecure class to create TLS connection
  WiFiClient client;
  Serial.print("connecting to ");
  Serial.println(host);
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return false;
  }
  String url2 =url + "/statechanged/" + state + "?access_token=" + accToken + "&ipadd=" + pingHost;

  Serial.print("requesting URL: ");
  Serial.println(url2);

  
  client.print(String("GET ") + url2 + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: BuildFailureDetectorESP8266\r\n" +
               "Connection: close\r\n\r\n");

  Serial.println("request sent");
  if(advancedDebug){
    while (client.connected()) {
      String line = client.readStringUntil('\n');
      if (line == "\r") {
        Serial.println("headers received");
        break;
      }
    }
    String line = client.readString();
    Serial.println("reply below (if blank this usually means successful:");
    Serial.println("==========");
    Serial.println(line);
    Serial.println("==========");
    Serial.println("closing connection");
  }
  return true;
}

This is all the code I use. I am trying to send the status of the pinged devices to my smartthings hub. Wen I manually put in the update URL(which is host+url2), the status of the device connected at the smarthings end gets updated. This update doesn't happen at all when the ESP tries to send the url.
I hope this clears things up a bit.

Are you sure 'url2' is supposed to start with "secondPartOfUrl"?

Try setting this to true. Then you can look for the "headers received" message on Serial Monitor. You might want to print out those headers, too by adding:
Serial.println(line);
between
String line = client.readStringUntil('\n');
and
if (line == "\r") {

This is something like /api/smartapps/installations/"personalCode"/ this should go after the initial host URL. I have tried putting "host + url + "/statechanged/" + state + "?access_token=" + accToken + "&ipadd=" + pingHost" This with the same outcome.

I have tried this.

reply below (if blank this usually means successful:
==========

==========

With this as a result. Again, same outcome. I'm beginning to think smartthings just doesn't like my esp for some reason.

Could it maybe be that the esp code uses http? When put into the browser it uses https. If so I need to change WiFiClient to WiFiClientSecure. The code was originally designed with this.

When going to WiFiClientSecure the esp doesn't even connect to the host anymore though. The client.connect results in 0, which means the connection has failed. I fixed this issue by using http instead of https.

Looking at the headers returned would help. You would get an error if you were being redirected to an HTTPS page.

url should start with /

I've found the problem was indeed that smartthings was not accepting hhtp and had to switch to https. This didn't work either at first. After using the SSL fingerprint of smartthings, it did connect and send the data correctly. Thanks anyway for trying to help!