gpt4 book ai didi

c++ - WinHTTP 以 unicode 请求数据?

转载 作者:行者123 更新时间:2023-11-28 07:07:52 25 4
gpt4 key购买 nike

我正在尝试通过 WinHTTP 读取网页:

bool WinHTTPClass::QueryResponseData(std::string &query_data)
{
// Read response

DWORD dwSize, dwDownloaded = 0;

do
{
// Check for available data.

if( !WinHttpQueryDataAvailable( hRequest, &dwSize ) )
{
cout << "Error querying data : " << GetLastError() << endl;
return false;
}

// Allocate space for the buffer.

char* pszOutBuffer = new char[dwSize+1];

if( !pszOutBuffer )
{
cout << "Out of memory" << endl;
dwSize=0;
}
else
{
// Read the data.
ZeroMemory( pszOutBuffer, dwSize+1 );

if( !WinHttpReadData( hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded ) )
{
cout << "Error reading data : " << GetLastError() << endl;
return false;
}
else
{
query_data += pszOutBuffer;
}

// Free the memory allocated to the buffer.
delete [] pszOutBuffer;
}
}
while( dwSize > 0 );

return true;
}

这一切都很好。我在这里遇到的困惑是我应该使用 unicode 编码缓冲区而不是:

char* pszOutBuffer = new char[dwSize+1];

比如用wchar_t代替网页常用的UTF8?有什么不同?我哪里糊涂了?

最佳答案

HTTP 是一种二进制传输,它没有文本或Unicode 的概念。 HTTP 使用 7 位 ASCII 作为 HTTP header ,但内容是任意二进制数据,其解释取决于描述它的 HTTP header ,最值得注意的是 Content-Type。 header 。所以您需要将原始内容数据接收到您的 char[] 中先缓冲,然后查看收到的 Content-Type header 使用 WinHttpQueryHeaders() 看看你收到了什么样的数据。如果它说你收到了 text/... type 然后标题通常也会指定 charset的文字。在text/html的情况下, charset可能在 <meta>标记在 HTML 本身而不是在 HTTP header 中。一旦你知道 charset的文本,然后您可以将其转换为 wchar_t[]使用 MultiByteToWideChar() (您必须手动查找字符集的适当代码页)。

关于c++ - WinHTTP 以 unicode 请求数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21503314/

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