title: TLS Client in MSVC++ for localhost:Port
body: <p>I have a local server which has a REST API running at <a href="https://localhost:61000" rel="nofollow noreferrer">https://localhost:61000</a>, to which I can send the following Curl request:</p>
  
  <pre><code>curl -X POST "https://localhost:61000/users/logout" -H "accept: */*" -H "Locale: en"
  </code></pre>
  
  <p>and it would return me the following response</p>
  
  <p><a href="https://i.stack.imgur.com/Xjrdz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Xjrdz.png" alt="enter image description here"></a></p>
  
  <p>In MSVC++ using the <code>WinSock</code> and <code>OpenSSL</code> library I can get the response code <code>200</code> from <code>google.com</code> at port <code>443</code>, but when I try for localhost at port 61000 I always get a <code>HTTP/1.1 400 Bad Request</code></p>
  
  <pre><code>// Client
  
  int main(int argc, char* argv[])
  {
      WSADATA wsadata;
      if ((WSAStartup(MAKEWORD(2, 0), &amp;wsadata)) == 0) {
          cout &lt;&lt; "-WSAStartup Initialized." &lt;&lt; endl;
          struct addrinfo hints, * res;
          int sockfd;
  
          memset(&amp;hints, 0, sizeof hints);
          hints.ai_socktype = 0;// SOCK_STREAM;
          hints.ai_protocol = 0;// IPPROTO_TCP;
          hints.ai_family = AF_UNSPEC;
  
          if (getaddrinfo("localhost", "61000", &amp;hints, &amp;res) != 0) {
              cout &lt;&lt; "-getaddrinfo unsuccessful." &lt;&lt; WSAGetLastError() &lt;&lt; endl;
          }
  
          if ((sockfd = socket(res-&gt;ai_family, res-&gt;ai_socktype, res-&gt;ai_protocol)) == INVALID_SOCKET) {
              cout &lt;&lt; "-Unable to create socket." &lt;&lt; WSAGetLastError() &lt;&lt; endl;
          }
  
          if ((connect(sockfd, res-&gt;ai_addr, res-&gt;ai_addrlen)) != SOCKET_ERROR) {
              cout &lt;&lt; "-Connection Established." &lt;&lt; endl;
          }
  
          cout &lt;&lt; "-Client connected to: " &lt;&lt; res-&gt;ai_addr &lt;&lt; endl;
  
          SSL_library_init();
          SSLeay_add_ssl_algorithms();
          SSL_load_error_strings();
  
          const SSL_METHOD* meth = TLSv1_2_client_method();
          SSL_CTX* ctx = SSL_CTX_new(meth);
  
          ssl = SSL_new(ctx);
          if (!ssl) {
              printf("Error creating SSL.\n");
              log_ssl();
              return -1;
          }
          sock = SSL_get_fd(ssl);
          SSL_set_fd(ssl, sockfd);
          int err = SSL_connect(ssl);
          if (err &lt;= 0) {
              printf("Error creating SSL connection.  err=%x\n", err);
              log_ssl();
              fflush(stdout);
              return -1;
          }
          printf("SSL connection using %s\n", SSL_get_cipher(ssl));
  
          const char* request = "GET /users HTTPS/1.1\r\n\r\n";
       //SendPacket
          int len = SSL_write(ssl, request, strlen(request));
          if (len &lt; 0) {
              int err = SSL_get_error(ssl, len);
              switch (err) {
              case SSL_ERROR_WANT_WRITE:
                  return 0;
              case SSL_ERROR_WANT_READ:
                  return 0;
              case SSL_ERROR_ZERO_RETURN:
              case SSL_ERROR_SYSCALL:
              case SSL_ERROR_SSL:
              default:
                  return -1;
              }
          }
  
          //RecvPacket
          len = 100;
          char buf[20000];
          do {
              len = SSL_read(ssl, buf, 100);
              buf[len] = 0;
              printf(buf);
          } while (len &gt; 0);
          if (len &lt; 0) {
              int err = SSL_get_error(ssl, len);
              if (err == SSL_ERROR_WANT_READ)
                  return 0;
              if (err == SSL_ERROR_WANT_WRITE)
                  return 0;
              if (err == SSL_ERROR_ZERO_RETURN || err == SSL_ERROR_SYSCALL || err == SSL_ERROR_SSL)
                  return -1;
          }
      }
      else {
          cout &lt;&lt; "-WSAStartup Initialization failed." &lt;&lt; endl;
          if (WSACleanup() != 0) {
              cout &lt;&lt; "-WSACleanup Successful." &lt;&lt; endl;
          }
          else {
              cout &lt;&lt; "-WSACleanup Failed." &lt;&lt; endl;
          }
      }
      return 0;
  }
  </code></pre>
  
  <p>Response:
  <a href="https://i.stack.imgur.com/LHwzg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LHwzg.png" alt="enter image description here"></a></p>
  
  <p>Assuming that the (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error, but I can not figure out how to create the request in right way.</p>