HTTP client example on win32

2020-05-20 09:00发布

问题:

I wanted to develop one HTTP example on win32 platform, which is asynchronous.

I am new to win32 programming, what are the api and library win32 platform provides for HTTP send and receive request? I am using Windows XP with VS 2005.

If any example is available please provide a link to it.

回答1:

You can use WinHTTP library. Here is an sample on Asynchronous completion.



回答2:

Window HTTP Services "provides developers with an HTTP client application programming interface (API) to send requests through the HTTP protocol to other HTTP servers."

HTTP Server API "enables applications to communicate over HTTP without using Microsoft Internet Information Server (IIS)"



回答3:

Generally I'd recommend something cross-platform like cURL, POCO, Qt, or Asio (pretty modern and nice). However, here is a Windows example using IXMLHTTPRequest:

// 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();


回答4:

Boost Asio is a nice synchronous/asynchronous library which has everything you need for HTTP servers/clients. It has some extensive examples on HTTP servers, and the relevant clients. Now if you are new to C++ in general this library may be a little cryptic. You could always go have a look at MSDN if you want a more from scratch approach.



回答5:

This is an example

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

A C++ light wrapper for POSIX and Winsock sockets

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.