How do you make a HTTP request with C++?

后端 未结 22 2509
有刺的猬
有刺的猬 2020-11-22 06:25

Is there any way to easily make a HTTP request with C++? Specifically, I want to download the contents of a page (an API) and check the contents to see if it contains a 1 o

22条回答
  •  孤独总比滥情好
    2020-11-22 06:59

    You can use ACE in order to do so:

    #include "ace/SOCK_Connector.h"
    
    int main(int argc, ACE_TCHAR* argv[])
    {
        //HTTP Request Header
        char* szRequest = "GET /video/nice.mp4 HTTP/1.1\r\nHost: example.com\r\n\r\n"; 
        int ilen = strlen(szRequest);
    
        //our buffer
        char output[16*1024];
    
        ACE_INET_Addr server (80, "example.com");
        ACE_SOCK_Stream peer;
        ACE_SOCK_Connector connector;
        int ires = connector.connect(peer, server);
        int sum = 0;
        peer.send(szRequest, ilen);
        while (true)
        {
            ACE_Time_Value timeout = ACE_Time_Value(15);
            int rc = peer.recv_n(output, 16*1024, &timeout);
            if (rc == -1)
            {
                break;
            }
            sum += rc;
        }
        peer.close();
        printf("Bytes transffered: %d",sum);
    
        return 0;
    }
    

提交回复
热议问题