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

后端 未结 22 2491
有刺的猬
有刺的猬 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 07:10

    Here is some code that will work with no need to use any 3rd party library: First define your gateway, user, password and any other parameters you need to send to this specific server.

    #define USERNAME "user"
    #define PASSWORD "your password"
    #define GATEWAY "your gateway"
    

    Here is the code itself:

    HINTERNET hOpenHandle, hResourceHandle, hConnectHandle;
    const TCHAR* szHeaders = _T("Content-Type:application/json; charset=utf-8\r\n");
    
    
    hOpenHandle = InternetOpen(_T("HTTPS"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    if (hOpenHandle == NULL)
    {
        return false;
    }
    
    
    hConnectHandle = InternetConnect(hOpenHandle,
        GATEWAY,
        INTERNET_DEFAULT_HTTPS_PORT,
        NULL, NULL, INTERNET_SERVICE_HTTP,
        0, 1);
    
    if (hConnectHandle == NULL)
    {
        InternetCloseHandle(hOpenHandle);
        return false;
    }
    
    
    hResourceHandle = HttpOpenRequest(hConnectHandle,
        _T("POST"),
        GATEWAY,
        NULL, NULL, NULL, INTERNET_FLAG_SECURE | INTERNET_FLAG_KEEP_CONNECTION,
        1);
    
    if (hResourceHandle == NULL)
    {
        InternetCloseHandle(hOpenHandle);
        InternetCloseHandle(hConnectHandle);
        return false;
    }
    
    InternetSetOption(hResourceHandle, INTERNET_OPTION_USERNAME, (LPVOID)USERNAME, _tcslen(USERNAME));
    InternetSetOption(hResourceHandle, INTERNET_OPTION_PASSWORD, (LPVOID)PASSWORD, _tcslen(PASSWORD));
    
    std::string buf;
    if (HttpSendRequest(hResourceHandle, szHeaders, 0, NULL, 0))
    {
        while (true)
        {
            std::string part;
            DWORD size;
            if (!InternetQueryDataAvailable(hResourceHandle, &size, 0, 0))break;
            if (size == 0)break;
            part.resize(size);
            if (!InternetReadFile(hResourceHandle, &part[0], part.size(), &size))break;
            if (size == 0)break;
            part.resize(size);
            buf.append(part);
        }
    }
    
    if (!buf.empty())
    {
        // Get data back
    }
    
    InternetCloseHandle(hResourceHandle);
    InternetCloseHandle(hConnectHandle);
    InternetCloseHandle(hOpenHandle);
    

    That should work on a Win32 API environment.

    Here is an example.

    0 讨论(0)
  • 2020-11-22 07:11

    libCURL is a pretty good option for you. Depending on what you need to do, the tutorial should tell you what you want, specifically for the easy handle. But, basically, you could do this just to see the source of a page:

    CURL* c;
    c = curl_easy_init();
    curl_easy_setopt( c, CURL_URL, "www.google.com" );
    curl_easy_perform( c );
    curl_easy_cleanup( c );
    

    I believe this will cause the result to be printed to stdout. If you want to handle it instead -- which, I assume, you do -- you need to set the CURL_WRITEFUNCTION. All of that is covered in the curl tutorial linked above.

    0 讨论(0)
  • 2020-11-22 07:13

    I had the same problem. libcurl is really complete. There is a C++ wrapper curlpp that might interest you as you ask for a C++ library. neon is another interesting C library that also support WebDAV.

    curlpp seems natural if you use C++. There are many examples provided in the source distribution. To get the content of an URL you do something like that (extracted from examples) :

    // Edit : rewritten for cURLpp 0.7.3
    // Note : namespace changed, was cURLpp in 0.7.2 ...
    
    #include <curlpp/cURLpp.hpp>
    #include <curlpp/Options.hpp>
    
    // RAII cleanup
    
    curlpp::Cleanup myCleanup;
    
    // Send request and get a result.
    // Here I use a shortcut to get it in a string stream ...
    
    std::ostringstream os;
    os << curlpp::options::Url(std::string("http://www.wikipedia.org"));
    
    string asAskedInQuestion = os.str();
    

    See the examples directory in curlpp source distribution, there is a lot of more complex cases, as well as a simple complete minimal one using curlpp.

    my 2 cents ...

    0 讨论(0)
  • 2020-11-22 07:15

    There is a newer, less mature curl wrapper being developed called C++ Requests. Here's a simple GET request:

    #include <iostream>
    #include <cpr.h>
    
    int main(int argc, char** argv) {
        auto response = cpr::Get(cpr::Url{"http://httpbin.org/get"});
        std::cout << response.text << std::endl;
    }
    

    It supports a wide variety of HTTP verbs and curl options. There's more usage documentation here.

    Disclaimer: I'm the maintainer of this library.

    0 讨论(0)
提交回复
热议问题