C ++ Windows HTTP

I'm looking for a basic tutorial for connecting to a domain and downloading an index file. Anyone who can link me to a good example or something.

+2


a source to share


5 answers


Check libCURL, it will do it for you.



+5


a source


The simplest solution is using URLDownloadToFile .

However, you can use all of these APIs together:



I'm sure there is another simple API for this, but I don't remember right now.

+2


a source


There is a free HTTP library containing Ultimate TCP / IP .

0


a source


I am using Poco . as a side effect, it is also portable (works with Linux and other OS as well).

void openHttpURL(string host, int port, string path)
{
    try
    {
        HTTPClientSession session(host, port);
    //  session.setTimeout(Timespan(connectionTimeout, 0));
        HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
        session.sendRequest(req);
        HTTPResponse res;
        int code = res.getStatus();
        if (code != res.HTTP_OK)
        {
            stringstream s;
            s << "HTTP Error " << code;
            throw Poco::IOException(s.str());
        }
        std::istream& rs = session.receiveResponse(res);
        int len = res.getContentLength();
        // READ DATA FROM THE STREAM HERE

    }
    catch (Exception& exc)
    {
        stringstream s;
        s << "Error connecting to http://" << host << ':' << port << "/" << path + " : " + exc.displayText();
        throw Poco::IOException(s.str());
    }
}

      

0


a source


In general, I would recommend something cross platform like cURL, POCO, or Qt. However, here's a Windows example !:

// TODO: error handling

#include <atlbase.h>
#include <msxml6.h>

HRESULT hr;
CComPtr<IXMLHTTPRequest> 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();

      

0


a source







All Articles