title: OpenSSL in C++ Socket Connection (HTTPS Client)
body: <p>i wrote an script, who can create an connection to an HTTP Server and shows the content of the website in the console. 
  Very easy.</p>
  
  <p>But i want to connect to an https server and do the same procedures. 
  I searched at google and didn't found what i searched.</p>
  
  <p>Please help me and give me an tutorial who can i use the openssl library.</p>
  
  <p>I tried myself on the openssl library, but the library is very complicated and difficult to understand.</p>
  
  <p>Here is my code of the http client:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;ctype.h&gt;
  #include &lt;cstring&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sstream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;openssl/ssl.h&gt;
  
  using namespace std;
  
  int sock;
  struct sockaddr_in client;
  int PORT = 80;
  
  int main(int argc, char const *argv[])
  {
      bzero(&amp;client, sizeof(client));
      client.sin_family = AF_INET;
      client.sin_port = htons( PORT );
      client.sin_addr.s_addr = inet_addr("172.16.0.6");
  
      sock = socket(AF_INET, SOCK_STREAM, 0);
  
      if (sock &lt; 0) {
          cout &lt;&lt; "Error creating socket." &lt;&lt; endl;
          exit(1);
      }
  
      if ( connect(sock, (struct sockaddr *)&amp;client, sizeof(client)) &lt; 0 ) {
          close(sock);
          cout &lt;&lt; "Could not connect" &lt;&lt; endl;
          exit(1);
      }
  
      stringstream ss;
      ss &lt;&lt; "GET /" &lt;&lt; "\r\n"
      &lt;&lt; "Host: 172.16.1.4\r\n"
      &lt;&lt; "Accept: application/json\r\n"
      &lt;&lt; "Connection: close"
      &lt;&lt; "\r\n\r\n";
      string request = ss.str();
  
      if (send(sock, request.c_str(), request.length(), 0) != (int)request.length()) {
          cout &lt;&lt; "Error sending request." &lt;&lt; endl;
          exit(1);
      }
  
      char cur;
      while ( read(sock, &amp;cur, 1) &gt; 0 ) {
          cout &lt;&lt; cur;
      }
  
      return 0;
  }
  </code></pre>