MFC对WinINet封装时的BUG,导致无法完整查到的协议头中的字段。懒得上报,谁有空了帮我报告,感谢。
以下是代码:(注意特别注释部分)
BOOL CHttpFile::QueryInfo(DWORD dwInfoLevel, CString& str,
LPDWORD lpdwIndex) const
{
ASSERT(dwInfoLevel <= HTTP_QUERY_MAX);
ASSERT_VALID(this);
ASSERT(m_hFile != NULL);
BOOL bRet;
DWORD dwLen = 0;
// ask for nothing to see how long the return really is
str.Empty();
if (HttpQueryInfo(m_hFile, dwInfoLevel, NULL, &dwLen, 0/* 这里应该传入 lpdwIndex,而非0*/))
bRet = TRUE;
else
{
// now that we know how long it is, ask for exactly that much
// space and really request the header from the API
LPTSTR pstr = str.GetBufferSetLength(dwLen / sizeof(TCHAR));
bRet = HttpQueryInfo(m_hFile, dwInfoLevel, pstr, &dwLen, lpdwIndex);
if (bRet)
str.ReleaseBuffer(dwLen / sizeof(TCHAR));
else
str.ReleaseBuffer(0);
}
return bRet;
}
这里应该传入 lpdwIndex,而非0*/))
bRet = TRUE;
else
{
// now that we know how long it is, ask for exactly that much
// space and really request the header from the API
LPTSTR pstr = str.GetBufferSetLength(dwLen / sizeof(TCHAR));
bRet = HttpQueryInfo(m_hFile, dwInfoLevel, pstr, &dwLen, lpdwIndex);
if (bRet)
str.ReleaseBuffer(dwLen / sizeof(TCHAR));
else
str.ReleaseBuffer(0);
}
return bRet;
}
如何重现:
用以下串作为HTTP请求头:
GET https://www.baidu.com/ HTTP/1.1
Accept-Encoding: gzip; q=1.0
Accept-Encoding: defalte;q=0.5
Host: www.baidu.com
然后调用 CHttpFile::QueryInfo(假设 p指向一个有效的CHttpFile对象):
CString s;
DWORD dwIndex = 0;
while( p->QueryInfo( s, &dwIndex ) )
TRACE( _T("%s\n"), (LPCTSTR)s );
你会发现只输出了 gzip; q=1.0 ,而第2个Accept-encoding 的 defalte;q=0.5没有输出。因为CHttpFile::QueryInfo函数里第一次调用HttpQueryInfo时没有指示要获取第几个名称相同的属性,哇啦哇啦一大堆,原因看MSDN吧:
https://msdn.microsoft.com/en-us/library/aa384238(VS.85).aspx
以下是改正后的代码:
BOOL CHttpFile::QueryInfo(DWORD dwInfoLevel, CString& str,
LPDWORD lpdwIndex) const
{
ASSERT(dwInfoLevel <= HTTP_QUERY_MAX);
ASSERT_VALID(this);
ASSERT(m_hFile != NULL);
BOOL bRet;
DWORD dwLen = 0;
// ask for nothing to see how long the return really is
str.Empty();
if( HttpQueryInfo( m_hFile, dwInfoLevel, NULL, &dwLen, lpdwIndex/*改正*/) )
bRet = TRUE;
else
{
// now that we know how long it is, ask for exactly that much
// space and really request the header from the API
LPTSTR pstr = str.GetBufferSetLength(dwLen / sizeof(TCHAR));
bRet = HttpQueryInfo(m_hFile, dwInfoLevel, pstr, &dwLen, lpdwIndex);
if (bRet)
str.ReleaseBuffer(dwLen / sizeof(TCHAR));
else
str.ReleaseBuffer(0);
}
return bRet;
}