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

后端 未结 22 2752
有刺的猬
有刺的猬 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:53

    The HTTP protocol is very simple, so it is very simple to write a HTTP client. Here is one

    https://github.com/pedro-vicente/lib_netsockets

    It uses HTTP GET to retrieve a file from a web server, both server and file are command line parameters. The remote file is saved to a local copy.

    Disclaimer: I am the author

    check http.cc https://github.com/pedro-vicente/lib_netsockets/blob/master/src/http.cc

    int http_client_t::get(const char *path_remote_file)
    {
      char buf_request[1024];
    
      //construct request message using class input parameters
      sprintf(buf_request, "GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", path_remote_file, m_server_ip.c_str());
    
      //send request, using built in tcp_client_t socket
      if (this->write_all(buf_request, (int)strlen(buf_request)) < 0)
      {
        return -1;
      }
    

    EDIT: edited URL

提交回复
热议问题