title: C to perform HTTPS requests with openssl
body: <p>I am new to C language and I am trying to perfom a simple SSL GET request with OpenSSL in Linux Ubuntu 18.04. I have this I found at <a href="https://stackoverflow.com/questions/22077802/simple-c-example-of-doing-an-http-post-and-consuming-the-response">Simple C example of doing an HTTP POST and consuming the response</a></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;openssl/ssl.h&gt;
  #include &lt;openssl/err.h&gt;
  #include &lt;openssl/bio.h&gt;
  
  #define HOST "www.google.com"
  #define PORT "443"
  
  int main() {
  
      //
      //  Initialize the variables
      //
      BIO* bio;
      SSL* ssl;
      SSL_CTX* ctx;
  
      //
      //   Registers the SSL/TLS ciphers and digests.
      //
      //   Basically start the security layer.
      //
      SSL_library_init();
  
      //
      //  Creates a new SSL_CTX object as a framework to establish TLS/SSL
      //  or DTLS enabled connections
      //
      ctx = SSL_CTX_new(SSLv23_client_method());
  
      //
      //  -&gt; Error check
      //
      if (ctx == NULL)
      {
          printf("Ctx is null\n");
      }
  
      //
      //   Creates a new BIO chain consisting of an SSL BIO
      //
      bio = BIO_new_ssl_connect(ctx);
  
      //
      //  Use the variable from the beginning of the file to create a 
      //  string that contains the URL to the site that you want to connect
      //  to while also specifying the port.
      //
      BIO_set_conn_hostname(bio, HOST ":" PORT);
  
      //
      //   Attempts to connect the supplied BIO
      //
      if(BIO_do_connect(bio) &lt;= 0)
      {
          printf("Failed connection\n");
          return 1;
      }
      else
      {
          printf("Connected\n");
      }
  
      //
      //  The bare minimum to make a HTTP request.
      //
      char* write_buf = "GET / HTTP/1.1\r\n"
                        "Host: " HOST "\r\n"
                        "Connection: close\r\n"
                        "\r\n";
  
      //
      //   Attempts to write len bytes from buf to BIO
      //
      if(BIO_write(bio, write_buf, strlen(write_buf)) &lt;= 0)
      {
          //
          //  Handle failed writes here
          //
          if(!BIO_should_retry(bio))
          {
              // Not worth implementing, but worth knowing.
          }
  
          //
          //  -&gt; Let us know about the failed writes
          //
          printf("Failed write\n");
      }
  
      //
      //  Variables used to read the response from the server
      //
      int size;
      char buf[1024];
  
      //
      //  Read the response message
      //
      for(;;)
      {
          //
          //  Get chunks of the response 1023 at the time.
          //
          size = BIO_read(bio, buf, 1023);
  
          //
          //  If no more data, then exit the loop
          //
          if(size &lt;= 0)
          {
              break;
          }
  
          //
          //  Terminate the string with a 0, to let know C when the string 
          //  ends.
          //
          buf[size] = 0;
  
          //
          //  -&gt;  Print out the response
          //
          printf("%s", buf);
      }
  
      //
      //  Clean after ourselves
      //
      BIO_free_all(bio);
      SSL_CTX_free(ctx);
  
      return 0;
  }
  </code></pre>
  
  <p>This code works well, but with some sites e.g. <em>ghostbin.co</em>, the connection fails... what is causing this?
  I compile the code using <code>gcc test.c -o test -lssl -lcrypto</code>.
  I'm using OpenSSL 1.1.1  11 Sep 2018</p>