HTTP client example on win32

混江龙づ霸主 提交于 2019-12-02 20:58:12

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

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)"

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!