C++ Windows HTTP

发布于 2024-08-31 06:42:38 字数 54 浏览 5 评论 0原文

我正在寻找有关连接到域和下载索引文件的基本教程。任何人都可以给我链接一个很好的例子或任何东西。

I'm looking for a basic tutorial for connecting to a domain and downloading the index file . Anyone that can link me a good example or anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

拥有 2024-09-07 06:42:38

查看 libCURL 它会为你做这件事。

Check out libCURL it will do this for you.

趁年轻赶紧闹 2024-09-07 06:42:38

最简单的解决方案是使用 URLDownloadToFile

但是,您可以一起使用所有这些 API:

我很确定还有另一个简单的 API,但我现在不记得了。

The simplest solution is using URLDownloadToFile.

However, you can use all these APIs together:

I'm pretty sure there's still another simple API for that, but I don't remember right now.

聆听风音 2024-09-07 06:42:38

Ultimate TCP/IP 中包含一个免费的 HTTP 库。

There is a free HTTP library included with Ultimate TCP/IP.

如果没有 2024-09-07 06:42:38

我使用 Poco 来实现这一点。另外一个好处是它还具有可移植性(也适用于 Linux 和其他操作系统)。

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

I use Poco for that. as a side benefir it's also portable (works on Linux and other OSs 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());
    }
}
对风讲故事 2024-09-07 06:42:38

一般来说,我会推荐一些跨平台的东西,比如 cURL、POCO 或 Qt。不过,这是一个 Windows 示例!:

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

Generally I'd recommend something cross-platform like cURL, POCO, or Qt. However, here is 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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文