|
这么一段程序,在模拟器上运行,想访问https类型的网址,但是到了HttpSendRequest函数时,总返回错误,请高手指点,下面是函数红色部分是出错的地方:
void CwininetTestDlg::OnBnClickedButton()
{
HINTERNET hSession;
HINTERNET hConnection;
HINTERNET hRequest;
hSession = InternetOpen(
L"",
INTERNET_OPEN_TYPE_DIRECT,
NULL,
NULL,
NULL );
if ( hSession == NULL )
{
AfxMessageBox(L"InternetOpen failed");
return;
}
BOOL res = FALSE;
DWORD timeout = 10000;
res = InternetSetOption( hSession, INTERNET_OPTION_CONNECT_TIMEOUT, &timeout, sizeof(DWORD) );
res = InternetSetOption( hSession, INTERNET_OPTION_SEND_TIMEOUT, &timeout, sizeof(DWORD) );
res = InternetSetOption( hSession, INTERNET_OPTION_RECEIVE_TIMEOUT, &timeout, sizeof(DWORD) );
hConnection = InternetConnect(
hSession,
L"https://mail.google.com",
443,
L"",
L"",
INTERNET_SERVICE_HTTP,
INTERNET_FLAG_SECURE,
(DWORD)(this) );
if ( ( hConnection == NULL ) && ( GetLastError() != ERROR_IO_PENDING ) )
{
AfxMessageBox( L"InternetConnect error.\n" );
return;
}
CString strUrlPath = L"";
hRequest = HttpOpenRequest(
hConnection,
L"GET",
strUrlPath.GetBuffer(),
NULL,
NULL,
NULL,
INTERNET_FLAG_SECURE|
INTERNET_FLAG_IGNORE_CERT_CN_INVALID|INTERNET_FLAG_IGNORE_CERT_DATE_INVALID,
//INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_NO_CACHE_WRITE,
(DWORD)(this) );
strUrlPath.ReleaseBuffer();
DWORD err = GetLastError();
if ((hRequest == NULL ) && ( GetLastError() != ERROR_IO_PENDING ) )
{
AfxMessageBox( L"HttpOpenRequest error.\n" );
return;
}
CString strHeader = L"APIVersion: 1.0.0\nAccept-Encoding: gzip\nCache-Control: no-cache, must-revalidate\nContent-Type: application/xml\nPragma: no-cache" ;
CString strData = L"" ;
BOOL sendRequestResult;
sendRequestResult = HttpSendRequest(
hRequest,
strHeader.GetBuffer(),
(DWORD)strHeader.GetLength(),
strData.GetBuffer(),
(DWORD)strData.GetLength() );
strHeader.ReleaseBuffer();
strData.ReleaseBuffer();
if ( !sendRequestResult )
{
AfxMessageBox( L"HttpSendRequest error.\n" );
return;
}
// 获取相应码
LPVOID lpBuffer = NULL;
DWORD dwBufferLen = 0;
BOOL result;
result = HttpQueryInfo( hRequest, HTTP_QUERY_STATUS_CODE, (LPVOID)lpBuffer, &dwBufferLen, NULL );
if ( GetLastError() != ERROR_INSUFFICIENT_BUFFER )
{
AfxMessageBox( L"HttpQueryInfo error.\n" );
return ;
}
lpBuffer = new char[dwBufferLen+1];
result = HttpQueryInfo( hRequest, HTTP_QUERY_STATUS_CODE, (LPVOID)lpBuffer, &dwBufferLen, NULL );
if ( ( result == FALSE ) || ( lpBuffer == NULL ) )
{
AfxMessageBox( L"HttpQueryInfo error.\n" );
return;
}
CString strStatusCode = (LPTSTR)lpBuffer;
delete lpBuffer;
// 获取相应头
result = HttpQueryInfo( hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID)lpBuffer, &dwBufferLen, NULL );
if ( GetLastError() != ERROR_INSUFFICIENT_BUFFER )
{
AfxMessageBox( L"HttpQueryInfo error.\n" );
return ;
}
lpBuffer = new char[dwBufferLen+1];
result = HttpQueryInfo( hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID)lpBuffer, &dwBufferLen, NULL );
if ( ( result == FALSE ) || ( lpBuffer == NULL ) )
{
AfxMessageBox( L"HttpQueryInfo error.\n" );
return;
}
CString strResHeader = (LPTSTR)lpBuffer;
delete lpBuffer;
// 获取相应体
CString strResponseData;
UINT nSize;
nSize = 0;
DWORD bytesRead = 0, allreads = 0;
BOOL success = TRUE;
do
{
unsigned char buffer[1024];
success = InternetReadFile( hRequest, buffer, 1024, &bytesRead );
if ( success && bytesRead > 0 )
{
CString str = (LPTSTR)buffer;
strResponseData += str;
}
allreads += bytesRead;
}
while( success && bytesRead > 0 );
}
|
|