然后重新WinHttpSendRequest;
源码:
// TODO: code your application's behavior here.
LPCTSTR pszHost = _T("www.myweb.net");
LPCTSTR pszPort =_T("9999");
LPCTSTR fileURL = _T("/BookFileSource/book file.xml");
LPCTSTR pszPassWord = _T("testpassword");
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL;
LPCTSTR szAcceptTypes[] = {_T("text/*"),NULL};
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen( L"WinHTTP/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0 );
if(!hSession) _tprintf(_T("WinHttpOpen error %d\n"), ::GetLastError());
// Specify an HTTP server.
if( hSession )
hConnect = WinHttpConnect( hSession, pszHost,
_ttoi(pszPort), 0 );
if(!hConnect) _tprintf(_T("WinHttpConnect error %d\n"), ::GetLastError());
// Create an HTTP request handle.
if( hConnect )
hRequest = WinHttpOpenRequest( hConnect, L"GET", fileURL,
NULL, WINHTTP_NO_REFERER,
szAcceptTypes,
WINHTTP_FLAG_SECURE);
if(!hRequest) _tprintf(_T("WinHttpOpenRequest error %d\n"), ::GetLastError());
if(hRequest)
{
//load and set ca
TCHAR szPath[256] = {0};
::GetModuleFileName(NULL, szPath, 255);
(_tcsrchr(szPath, _T('\\')))[1] = 0;
CString strFile(szPath);
strFile.Append(_T("ca.p12"));
CFile file;
if(!file.Open(strFile, CFile::modeRead))
{
_tprintf(_T("Open ca file error %d\n"), ::GetLastError());
goto END;
}
std::vector clientPfx(file.GetLength(), 0);
file.Read(&clientPfx[0], file.GetLength());
file.Close();
// Convert a .pfx or .p12 file image to a Certificate store
CRYPT_DATA_BLOB PFX;
PFX.pbData= (BYTE *)&clientPfx[0];
PFX.cbData= clientPfx.size();
HCERTSTORE pfxStore= ::PFXImportCertStore( &PFX, pszPassWord, 0 );
if ( NULL == pfxStore )
{
_tprintf(_T("PFXImportCertStore error %d\n"), ::GetLastError());
goto END;
}
// Extract the certificate from the store and pass it to WinHttp
PCCERT_CONTEXT pcontext= NULL, clientCertContext = NULL;
while ( pcontext = ::CertEnumCertificatesInStore( pfxStore, pcontext ) ){
clientCertContext= ::CertDuplicateCertificateContext( pcontext ); // CertEnumCertificatesInStore frees its passed in pcontext !
BOOL stat= ::WinHttpSetOption( hRequest, WINHTTP_OPTION_CLIENT_CERT_CONTEXT, (LPVOID)clientCertContext, sizeof(CERT_CONTEXT) );
if ( FALSE == stat )
{
_tprintf(_T("WinHttpSetOption error %d\n"), ::GetLastError());
CertCloseStore(pfxStore,0);
CertFreeCertificateContext(clientCertContext);
goto END;
}
else
{
break;//success
}
}
CertCloseStore(pfxStore,0);
CertFreeCertificateContext(clientCertContext);
// Certain circumstances dictate that we may need to loop on WinHttpSendRequest
// hence the do/while
bool retry = false;
int result = NO_ERROR;
do
{
retry = false;
result = NO_ERROR;
// no retry on success, possible retry on failure
if(WinHttpSendRequest(
hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
0,
0,
0,
0,
NULL
) == FALSE)
{
result = GetLastError();
// (1) If you want to allow SSL certificate errors and continue
// with the connection, you must allow and initial failure and then
// reset the security flags. From: "HOWTO: Handle Invalid Certificate
// Authority Error with WinInet"
// http://support.microsoft.com/default.aspx?scid=kb;EN-US;182888
if(result == ERROR_WINHTTP_SECURE_FAILURE)
{
DWORD dwFlags =
SECURITY_FLAG_IGNORE_UNKNOWN_CA |
SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE |
SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
if(WinHttpSetOption(
hRequest,
WINHTTP_OPTION_SECURITY_FLAGS,
&dwFlags,
sizeof(dwFlags)))
{
retry = true;
}
}
// (2) Negotiate authorization handshakes may return this error
// and require multiple attempts
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa383144%28v=vs.85%29.aspx
else if(result == ERROR_WINHTTP_RESEND_REQUEST)
{
retry = true;
}
}
else
{
bResults = TRUE;
}
} while(retry);
}
// End the request.
if( bResults )
bResults = WinHttpReceiveResponse( hRequest, NULL );
// Keep checking for data until there is nothing left.
if( bResults )
{
do
{
// Check for available data.
dwSize = 0;
if( !WinHttpQueryDataAvailable( hRequest, &dwSize ) )
{
TRACE( "Error %u in WinHttpQueryDataAvailable.\n",
GetLastError( ) );
}
// Allocate space for the buffer.
pszOutBuffer = new char[dwSize+1];
if( !pszOutBuffer )
{
TRACE( "Out of memory\n" );
dwSize=0;
}
else
{
// Read the data.
ZeroMemory( pszOutBuffer, dwSize+1 );
if( !WinHttpReadData( hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded ) )
TRACE( "Error %u in WinHttpReadData.\n", GetLastError( ) );
else
TRACE( "%s", pszOutBuffer );
// Free the memory allocated to the buffer.
delete [] pszOutBuffer;
}
} while( dwSize > 0 );
}
// Report any errors.
if( !bResults )
TRACE( "Error %d has occurred.\n", GetLastError( ) );
WinHttpSetStatusCallback( hSession,
NULL,
WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS,
NULL );
END:
// Close any open handles.
if( hRequest ) WinHttpCloseHandle( hRequest );
if( hConnect ) WinHttpCloseHandle( hConnect );
if( hSession ) WinHttpCloseHandle( hSession );