gpt4 book ai didi

c++ - C++/WinAPI 中的 .NET WebClient.DownloadData(url) 替代方案?

转载 作者:行者123 更新时间:2023-11-28 03:51:01 26 4
gpt4 key购买 nike

如何使用 C++ 在线获取文件的内容?

最佳答案

有多种方法可以做到这一点。

WinInet

首先,Windows 有一个内置的 API,允许您发出 HTTP 请求,使用起来相当简单。我使用这个简单的包装类来下载文件:

/**
* Simple wrapper around the WinInet library.
*/
class Inet
{
public:
explicit Inet() : m_hInet(NULL), m_hConnection(NULL)
{
m_hInet = ::InternetOpen(
"My User Agent",
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
/*INTERNET_FLAG_ASYNC*/0);
}

~Inet()
{
Close();

if (m_hInet)
{
::InternetCloseHandle(m_hInet);
m_hInet = NULL;
}
}

/**
* Attempt to open a URL for reading.
* @return false if we don't have a valid internet connection, the url is null, or we fail to open the url, true otherwise.
*/
bool Open(LPCTSTR url)
{
if (m_hInet == NULL)
{
return false;
}

if (url == NULL)
{
return false;
}

m_hConnection = ::InternetOpenUrl(
m_hInet,
url,
NULL /*headers*/,
0 /*headers length*/,
INTERNET_FLAG_NO_AUTH | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI,
reinterpret_cast<DWORD_PTR>(this));

return m_hConnection != NULL;
}

/**
* Read from a connection opened with Open.
* @return true if we read data.
*/
bool ReadFile(LPVOID lpBuffer, DWORD dwNumberOfBytesToRead, LPDWORD dwRead)
{
ASSERT(m_hConnection != NULL);

return ::InternetReadFile(m_hConnection, lpBuffer, dwNumberOfBytesToRead, dwRead) != 0;
}

/**
* Close any open connection.
*/
void Close()
{
if (m_hConnection != NULL)
{
::InternetCloseHandle(m_hConnection);
m_hConnection = NULL;
}
}

private:
HINTERNET m_hInet;
HINTERNET m_hConnection;
};

这个的用法很简单:

Inet inet;
if (inet.Open(url))
{
BYTE buffer[UPDATE_BUFFER_SIZE];
DWORD dwRead;
while (inet.ReadFile(&buffer[0], UPDATE_BUFFER_SIZE, &dwRead))
{
// TODO: Do Something With buffer here
if (dwRead == 0)
{
break;
}
}
}

LibCurl

如果您宁愿避免使用特定于 Windows 的 API,那么您可能会比使用 libcurl 做得更糟使用各种协议(protocol)(包括 HTTP)获取文件的库。有一个很好的示例展示了如何将 URL 直接检索到内存中(避免下载到磁盘):getinmemory sample .

关于c++ - C++/WinAPI 中的 .NET WebClient.DownloadData(url) 替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5484407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com