C++ win32 上的 HTTP 客户端示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1107862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:49:01  来源:igfitidea点击:

HTTP client example on win32

c++windowshttpasynchronouswindows-xp

提问by Vinayaka Karjigi

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

我想在win32平台上开发一个异步的HTTP示例。

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.

我是win32编程新手,win32平台为HTTP发送和接收请求提供了哪些api和库?我在 VS 2005 上使用 Windows XP。

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

如果有任何示例可用,请提供链接。

采纳答案by Canopus

You can use WinHTTP library. Hereis an sample on Asynchronous completion.

您可以使用 WinHTTP 库。是异步完成的示例。

回答by Brandon E Taylor

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

Window HTTP Services“为开发人员提供了一个 HTTP 客户端应用程序编程接口 (API),可以通过 HTTP 协议向其他 HTTP 服务器发送请求。”

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

HTTP Server API“使应用程序能够在不使用 Microsoft Internet Information Server (IIS) 的情况下通过 HTTP 进行通信”

回答by Peter Tseng

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:

一般来说,我会推荐一些跨平台的东西,比如 cURL、POCO、Qt 或 Asio(非常现代和漂亮)。但是,这是一个 Windows 示例,使用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();

回答by DeusAduro

Boost Asiois 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.

Boost Asio是一个不错的同步/异步库,它具有 HTTP 服务器/客户端所需的一切。它有一些关于 HTTP 服务器和相关客户端的广泛示例。现在,如果您一般不熟悉 C++,这个库可能有点神秘。如果您想要更多从头开始的方法,您可以随时查看 MSDN。

回答by Pedro Vicente

This is an example

这是一个例子

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

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

A C++ light wrapper for POSIX and Winsock sockets

POSIX 和 Winsock 套接字的 C++ 轻量级包装器

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.

它使用 HTTP GET 从 Web 服务器检索文件,服务器和文件都是命令行参数。远程文件保存到本地副本。