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

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

    Generally I'd recommend something cross-platform like cURL, POCO, or Qt. However, here is a Windows example!:

    #include 
    #include 
    #include  // _bstr_t
    
    HRESULT hr;
    CComPtr request;
    
    hr = request.CoCreateInstance(CLSID_XMLHTTP60);
    hr = request->open(
        _bstr_t("GET"),
        _bstr_t("https://www.google.com/images/srpr/logo11w.png"),
        _variant_t(VARIANT_FALSE),
        _variant_t(),
        _variant_t());
    hr = request->send(_variant_t());
    
    // get status - 200 if succuss
    long status;
    hr = request->get_status(&status);
    
    // load image data (if url points to an image)
    VARIANT responseVariant;
    hr = request->get_responseStream(&responseVariant);
    IStream* stream = (IStream*)responseVariant.punkVal;
    CImage *image = new CImage();
    image->Load(stream);
    stream->Release();
    

提交回复
热议问题