gpt4 book ai didi

c++ - 如何使用 winhttp 获取请求 cookie?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:49:27 33 4
gpt4 key购买 nike

响应 cookie('Set-Cookie' 响应 header )获得良好,但请求 cookie('Cookie' 请求 header )我无法通过 WinHttpQueryHeaders 获得,即使使用 WINHTTP_QUERY_FLAG_REQUEST_HEADERS:

DWORD size = 0;
BOOL re = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_COOKIE /*| WINHTTP_QUERY_FLAG_REQUEST_HEADERS*/, nullptr, nullptr, &size, nullptr);
DWORD err = GetLastError(); // re = 0 && err == ERROR_WINHTTP_HEADER_NOT_FOUND

怎么了?

最佳答案

这应该有效:

DWORD lastError;
LPVOID lpBuffer;
DWORD dwSize = 0, dwIndex = 0;

// Call with zero size to get actual size
BOOL res = WinHttpQueryHeaders(hRequest,
WINHTTP_QUERY_COOKIE,
WINHTTP_HEADER_NAME_BY_INDEX,
NULL,
&dwSize,
&dwIndex);
lastError = GetLastError();
if (lastError == ERROR_INSUFFICIENT_BUFFER)
{
lpBuffer = new WCHAR[dwSize/sizeof(WCHAR)];
WinHttpQueryHeaders(hRequest,
WINHTTP_QUERY_COOKIE,
WINHTTP_HEADER_NAME_BY_INDEX,
lpBuffer,
&dwSize,
&dwIndex);

// convert result to wstring
std::wstring result(reinterpret_cast<wchar_t*>(lpBuffer),
dwSize/sizeof(wchar_t));
std::wcout << L"Result: " << result << endl;
delete[] lpBuffer;
}

关于c++ - 如何使用 winhttp 获取请求 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31759787/

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