0

I found an example here but I keep getting the following error, I have tried various combinations in the header to no avail... Postman requests work fine. I am missing something... This is a 443 port... Do I need to use another method? As you will discover reading on I am not familiar with making HTTPS requests but need to add this to my software to pull recent data from a server. Any assistance would be appreciated...

HTTP/1.1 400 Bad Request Server: awselb/2.0 Date: Mon, 25 May 2020 16:52:31 GMT Content-Type: text/html Content-Length: 236 Connection: close

#include <windows.h>
#include <string>
#include <stdio.h>
#include <winsock2.h>
#include <WS2tcpip.h>
#include <windows.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
using std::string;

... 


    // Initialize Dependencies to the Windows Socket.
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
        fprintf(fpSessionLog,"WSAStartup failed.\n");
        fflush(fpSessionLog);
        goto SKIP;
    }

    // We first prepare some "hints" for the "getaddrinfo" function
    // to tell it, that we are looking for a IPv4 TCP Connection.
    struct addrinfo hints;
    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_INET;          // We are targeting IPv4
    hints.ai_protocol = IPPROTO_TCP;    // We are targeting TCP
    hints.ai_socktype = SOCK_STREAM;    // We are targeting TCP so its SOCK_STREAM

    // Aquiring of the IPv4 address of a host using the newer
    // "getaddrinfo" function which outdated "gethostbyname".
    // It will search for IPv4 addresses using the TCP-Protocol.
    struct addrinfo* targetAdressInfo = NULL;
    DWORD getAddrRes = getaddrinfo("api-thisSite.com", NULL, &hints, &targetAdressInfo);
    if (getAddrRes != 0 || targetAdressInfo == NULL)
    {
        fprintf(fpSessionLog,"Could not resolve the Host Name\n");
        fflush(fpSessionLog);
        WSACleanup();
        goto SKIP;
    }

    // Create the Socket Address Informations, using IPv4
    // We dont have to take care of sin_zero, it is only used to extend the length of SOCKADDR_IN to the size of SOCKADDR
    SOCKADDR_IN sockAddr;
    sockAddr.sin_addr = ((struct sockaddr_in*) targetAdressInfo->ai_addr)->sin_addr;    // The IPv4 Address from the Address Resolution Result
    sockAddr.sin_family = AF_INET;  // IPv4
    sockAddr.sin_port = htons(443);  

    // We have to free the Address-Information from getaddrinfo again
    freeaddrinfo(targetAdressInfo);

    // Creation of a socket for the communication with the Web Server,
    // using IPv4 and the TCP-Protocol
    SOCKET webSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (webSocket == INVALID_SOCKET)
    {
        fprintf(fpSessionLog,"Creation of the Socket Failed\n");
        fflush(fpSessionLog);
        WSACleanup();
        goto SKIP;
    }

    // Establishing a connection to the web Socket
    //cout << "Connecting...\n";
    fprintf(fpSessionLog,"Connecting...\n");
    fflush(fpSessionLog);

    if(connect(webSocket, (SOCKADDR*)&sockAddr, sizeof(sockAddr)) != 0)
    {
       fprintf(fpSessionLog,"Could not connect!\n");
       fflush(fpSessionLog);
       closesocket(webSocket);
       WSACleanup();
       goto SKIP;
    }
    fprintf(fpSessionLog,"Connected!\n");
    fflush(fpSessionLog);

    // Sending a HTTP-GET-Request to the Web Server  
    const char* httpRequest = "GET /api/v1/ecm HTTP/1.1\r\nHost: api-thisSite.com\r\nX-Authorization: {api key goes here}\r\nAccept: application/json"
    fprintf(fpSessionLog,"Formulated Server Request: %s \n",httpRequest);
    fflush(fpSessionLog);
    int sentBytes = send(webSocket, httpRequest, strlen(httpRequest),0);
    if (sentBytes < strlen(httpRequest) || sentBytes == SOCKET_ERROR)
    {
        //cout << "Could not send the request to the Server" << endl;
        //system("pause");
        fprintf(fpSessionLog,"Could not send the request to the Server!\n");
        fflush(fpSessionLog);
        closesocket(webSocket);
        WSACleanup();
        goto SKIP;
    }

    // Receiving and Displaying an answer from the Web Server
    char buffer[10000];
    ZeroMemory(buffer, sizeof(buffer));
    int dataLen;
    while ((dataLen = recv(webSocket, buffer, sizeof(buffer), 0) > 0))
    {
        int i = 0;
        while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
            //cout << buffer[i];
            fprintf(fpSessionLog,"%c",buffer[i]);
            i += 1;
        }
    }
    fflush(fpSessionLog);

    // Cleaning up Windows Socket Dependencies
    closesocket(webSocket);
    WSACleanup();









    SKIP:



HTTP/1.1 400 Bad Request
Server: awselb/2.0
Date: Mon, 25 May 2020 20:32:40 GMT
Content-Type: text/html
Content-Length: 236
Connection: close

<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
</body>
</html>

How do I send an HTTPS request to an HTTPS port?

3
  • You will not be able to write your own HTTPS implementation in any reasonable amount of time. Use an existing library. Windows comes with two of them - WinInet and WinHTTP. Either is capable of sending HTTPS requests. May 25, 2020 at 23:54
  • HTTPS is more than just connecting on TCP port 443. Much, much, much, much, more. You have to implement the TLS protocol. All 100 pages of it. May 26, 2020 at 1:50
  • Thanks, I git it working with WinINet...
    – DonL515
    May 26, 2020 at 19:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.