HTTP GET/POST in native C++ RRS feed

  • Question

  • Hello,

        I try to make work the TVBDApi in C++, but I got some problem and I didn't get why? Actually, I make it work with powershell with curl, but I won't use libCurl for this example.

    There is the sample code:

    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <windows.h>
    #include <iostream>
    #include <sstream>
    using namespace std;
    #pragma comment(lib,"ws2_32.lib")
    SOCKET OpenConnection(const char* hostname, const char* Port);
    int SendHTTP(SOCKET Socket, const char* Command);
    void ReceiveHTTP(SOCKET Socket, string& Response);
    int main(void) 
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(2, 2), &wsaData)) return 1;
        SOCKET Socket;
        string url = "api.thetvdb.com";
        Socket = OpenConnection(url.c_str(), "443");
        if (Socket == -1) return 1;
        string Response;
        //string get_http = "GET / HTTP/1.1\r\nHost: " + url + "\r\nConnection: close\r\n\r\n";
        string Command = "\r\n{\r\n\"apikey\": \"APIKEYHERE\",\r\n\"userkey\" : \"USERKEYHERE\",\r\n\"username\" : \"USERNAMEHERE\"\r\n}\r\n\r\n";
        string get_http = "POST /login HTTP/1.1\r\nHost:" + url + "\r\nContent-Type:application/json\r\nAccept:application/json\r\nContent-Length:" + to_string(Command.length()) + Command;
        SendHTTP(Socket, get_http.c_str());
        ReceiveHTTP(Socket, Response);
        printf("%s",  Response.c_str());
        return 0;
    SOCKET OpenConnection(const char* hostname, const char* Port)
        struct addrinfo hints = {}, * addrs;
        hints.ai_family = AF_INET; 
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = IPPROTO_TCP;
        SOCKET sd;
        if (getaddrinfo(hostname, Port, &hints, &addrs))
            return -1;
        for (struct addrinfo* addr = addrs; addr != NULL; addr = addr->ai_next)
            if ((sd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol)) == -1) 
            if (connect(sd, addr->ai_addr, (int)addr->ai_addrlen) == 0) 
        return sd;
    int SendHTTP(SOCKET Socket, const char* Command)
        return send(Socket, Command, (int)strlen(Command), 0);
    void ReceiveHTTP(SOCKET Socket, string& Response)
        char buffer[10000];
        while ((recv(Socket, buffer, 10000, 0)) > 0)
            int i = 0;
            while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r')
                Response += buffer[i++];

    The Post command was:

    POST /login HTTP/1.1
    "apikey": "SCRAMBLEAPI",
    "userkey" : "SCRAMBLEKEY",
    "username" : "SCRAMBLEUSER"

    and I receive this from server:

    HTTP/1.1 400 Bad Request
    Server: CloudFront
    Date: Wed, 11 Mar 2020 15:52:32 GMT
    Content-Type: text/html
    Content-Length: 915
    Connection: close
    X-Cache: Error from cloudfront
    Via: 1.1 6806abb26fb4ce0aa6ebca12d6a79247.cloudfront.net (CloudFront)
    X-Amz-Cf-Pop: YTO50-C2
    X-Amz-Cf-Id: s9TbxHWxUMFr4gwpvE-ndMKQkzXtdnxgPk_bbwMK_cl3JVTpdvVEnA==
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
    <TITLE>ERROR: The request could not be satisfied</TITLE>
    <H1>400 ERROR</H1>
    <H2>The request could not be satisfied.</H2>
    <HR noshade size="1px">
    Bad request.
    We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
    <BR clear="all">
    If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
    <BR clear="all">
    <HR noshade size="1px">
    Generated by cloudfront (CloudFront)
    Request ID: s9TbxHWxUMFr4gwpvE-ndMKQkzXtdnxgPk_bbwMK_cl3JVTpdvVEnA==
    Sortie de E:\Home\Visual Studio 2019\Projects\HTTPREQ\x64\Debug\HTTPREQ.exe (processus 17596). Code : 0.
    Appuyez sur une touche pour fermer cette fenêtre. . .

    Does someone understand what wrong happen? Working well in TVDBAPI website and powershell.


    Wednesday, March 11, 2020 3:54 PM

All replies

  • Hi,

    Thank you for posting here.

    >> I try to make work the TVBDApi in C++, but I got some problem and I didn't get why? 

    This forum is about c++ development. According to your issue , I suggest you should ask it in the api-thetvdb forum. And this thread will be moved to Off-Topic Posts.

    Best Regards,

    Jeanine Zhang

    Thursday, March 12, 2020 1:59 AM
  • Oh my!!!

        I think it's a mistake to put the subject Off-Topic Posts and I will explain why.

        First, like you mention : "This forum is about c++ development." and OH YEAH! It's exactly what I'm! No library or DLL dependency and No Framework or .NET Stuff... Just me, Win32, DirectX and my old friends Memory allocation, pointer and reference. 

         Then if you just remove the context, I think we face a socket problem or I missing something with cloudflare? I didn't know.

         But in general the question is : "How made HTML GET/POST with Winsock2.h".

       Second, I've test my RAW request

    POST /login HTTP/1.1
    "apikey": "SCRAMBLEAPI",
    "userkey" : "SCRAMBLEKEY",
    "username" : "SCRAMBLEUSER"


    I receive my Token : no bad request

        "token": "eyJhbGciOIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODQ2MjY3OTIsImlkIjoiSFRUsIm9yaWdfaWF0IjoxNTg0MDIxOTkyLCJ1c2VyaWQiOjIxMDMxNywidXNlcm5hbWUiOiJtYXRoc3RsYXVyZW50NGloIn0.lplm3vPkHR0fpXDaEZf5OvCDcZKetBANtDF3zMv12accsGhatMTY-X0HT0sQyOi7NTuBQ4_rx1l19Zvt1RXPf0D91V4xqT5PpJ3Ez2bMCQ5ayNRw7hjgHcXjUN3eMTXrjpHZXWiWoV9gsmWwvGpzHzHCEwYblOTquNau7xPqMX6iyaJX9aVKog_CWxwKhi8b8DtrjR3lctSJ1kUz4PFwqhCN90moTOXXDyh-VACPQV_sjX09Od_MRKeamwOK2nZtXhzuF0PyAW6PxIqZRE7O1FsNRPVu61V9o3eZJ3_weIOX1nP473xD_KIAgLpqh9vsedjy7IVLeXdU2kBsLuCCcw"

    I can ask to thetvdb.org but I don't think these people was qualify in c++.

    Another Ping-Pong Case???

    • Edited by Fleµve Thursday, March 12, 2020 2:22 PM
    Thursday, March 12, 2020 2:15 PM