winhttpsendrequest POST 在 windows 服务器 2008 rc2 上使用 https
winhttpsendrequest POST with https on windows server 2008 rc2
我正在编写一个 visual c++ 应用程序,它应该将 http post 消息发送到 https 端点(https 服务器的证书是自签名的)。有 2 组客户端,一组在 windows 7 台电脑上,另一组在 windows 服务器 2008 rc2 上。使用 mmc console/snap-in(在两个 pc 组上)导入服务器的自签名证书后,windows 7 台电脑可以正确连接,但在 Windows 服务器 2008 rc2 机器上, winhttpsendrequest() 调用总是返回错误 12030。任何想法如何解决这个问题?任何 help/tip 将不胜感激。提前致谢。我的代码在下面。
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
hSession = WinHttpOpen( userAgent,
WINHTTP_ACCESS_TYPE_NO_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0 );
if( hSession ){
hConnect = WinHttpConnect( hSession, hostname, port, 0 );
if( hConnect ){
hRequest = WinHttpOpenRequest( hConnect, L"POST", urlPath,
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_REFRESH|WINHTTP_FLAG_SECURE );
if(hRequest){
AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
AutoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP|WINHTTP_AUTO_DETECT_TYPE_DNS_A;
AutoProxyOptions.fAutoLogonIfChallenged = TRUE;
if( WinHttpGetProxyForUrl( hSession,
fullURL,
&AutoProxyOptions,
&ProxyInfo))
{
printf("proxy info\n");
printf("access type: %ld\n",ProxyInfo.dwAccessType);
printf("list: %s\n",ProxyInfo.lpszProxy);
printf("bypass list: %s\n",ProxyInfo.lpszProxyBypass);
if( !WinHttpSetOption( hRequest,
WINHTTP_OPTION_PROXY,
&ProxyInfo,
cbProxyInfoSize ) )
{
printf("Proxy detection logic failed\n");
goto Exit;
}
}
else
{
printf("WinHttpGetProxyForUrl() returned [%d].. no proxy is used?\n", GetLastError());
}
bResults = WinHttpSendRequest(hRequest, L"Content-Type:application/x-www-form-urlencoded", 0, (LPVOID)postRequest, data_len, data_len, 0);
if ( bResults != TRUE) {
wprintf(L"WinHttpSendRequest failed (%d)\n", GetLastError());
}
else
{
//all good
bResults = WinHttpReceiveResponse( hRequest, NULL );
if( bResults )
{
do
{
dwSize = 0;
if( !WinHttpQueryDataAvailable( hRequest, &dwSize ) )
printf( "Error %u in WinHttpQueryDataAvailable.\n", GetLastError());
pszOutBuffer = new char[dwSize+1];
if( !pszOutBuffer )
{
printf( "Out of memory\n" );
dwSize=0;
}
else
{
ZeroMemory( pszOutBuffer, dwSize+1 );
if( !WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded ) )
printf( "Error %u in WinHttpReadData.\n", GetLastError( ) );
else{
printf( "From server [");
printf( "%s", pszOutBuffer );
printf("]\n");
}
delete [] pszOutBuffer;
}
} while( dwSize > 0 );
}
}
}
else
{
wprintf(L"WinHttpOpenRequest failed (%d)\n", GetLastError());
}
}
}
12030是这个:
ERROR_INTERNET_CONNECTION_ABORTED
12030
The connection with the server has been terminated.
这似乎不仅仅是身份验证或证书错误。
您是否尝试过检查您的服务器日志以了解它是如何处理连接的?使用 Fiddler?
观察流量
要排除身份验证步骤的问题,请尝试传递 here 中讨论的一些 SECURITY_FLAG_IGNORE_CERT_* 标志。
此外,请考虑使用 WinInet APIs 而不是 WinHTTP。使用前者更容易执行某些操作。
我正在编写一个 visual c++ 应用程序,它应该将 http post 消息发送到 https 端点(https 服务器的证书是自签名的)。有 2 组客户端,一组在 windows 7 台电脑上,另一组在 windows 服务器 2008 rc2 上。使用 mmc console/snap-in(在两个 pc 组上)导入服务器的自签名证书后,windows 7 台电脑可以正确连接,但在 Windows 服务器 2008 rc2 机器上, winhttpsendrequest() 调用总是返回错误 12030。任何想法如何解决这个问题?任何 help/tip 将不胜感激。提前致谢。我的代码在下面。
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
hSession = WinHttpOpen( userAgent,
WINHTTP_ACCESS_TYPE_NO_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0 );
if( hSession ){
hConnect = WinHttpConnect( hSession, hostname, port, 0 );
if( hConnect ){
hRequest = WinHttpOpenRequest( hConnect, L"POST", urlPath,
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_REFRESH|WINHTTP_FLAG_SECURE );
if(hRequest){
AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
AutoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP|WINHTTP_AUTO_DETECT_TYPE_DNS_A;
AutoProxyOptions.fAutoLogonIfChallenged = TRUE;
if( WinHttpGetProxyForUrl( hSession,
fullURL,
&AutoProxyOptions,
&ProxyInfo))
{
printf("proxy info\n");
printf("access type: %ld\n",ProxyInfo.dwAccessType);
printf("list: %s\n",ProxyInfo.lpszProxy);
printf("bypass list: %s\n",ProxyInfo.lpszProxyBypass);
if( !WinHttpSetOption( hRequest,
WINHTTP_OPTION_PROXY,
&ProxyInfo,
cbProxyInfoSize ) )
{
printf("Proxy detection logic failed\n");
goto Exit;
}
}
else
{
printf("WinHttpGetProxyForUrl() returned [%d].. no proxy is used?\n", GetLastError());
}
bResults = WinHttpSendRequest(hRequest, L"Content-Type:application/x-www-form-urlencoded", 0, (LPVOID)postRequest, data_len, data_len, 0);
if ( bResults != TRUE) {
wprintf(L"WinHttpSendRequest failed (%d)\n", GetLastError());
}
else
{
//all good
bResults = WinHttpReceiveResponse( hRequest, NULL );
if( bResults )
{
do
{
dwSize = 0;
if( !WinHttpQueryDataAvailable( hRequest, &dwSize ) )
printf( "Error %u in WinHttpQueryDataAvailable.\n", GetLastError());
pszOutBuffer = new char[dwSize+1];
if( !pszOutBuffer )
{
printf( "Out of memory\n" );
dwSize=0;
}
else
{
ZeroMemory( pszOutBuffer, dwSize+1 );
if( !WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded ) )
printf( "Error %u in WinHttpReadData.\n", GetLastError( ) );
else{
printf( "From server [");
printf( "%s", pszOutBuffer );
printf("]\n");
}
delete [] pszOutBuffer;
}
} while( dwSize > 0 );
}
}
}
else
{
wprintf(L"WinHttpOpenRequest failed (%d)\n", GetLastError());
}
}
}
12030是这个:
ERROR_INTERNET_CONNECTION_ABORTED
12030
The connection with the server has been terminated.
这似乎不仅仅是身份验证或证书错误。
您是否尝试过检查您的服务器日志以了解它是如何处理连接的?使用 Fiddler?
观察流量要排除身份验证步骤的问题,请尝试传递 here 中讨论的一些 SECURITY_FLAG_IGNORE_CERT_* 标志。
此外,请考虑使用 WinInet APIs 而不是 WinHTTP。使用前者更容易执行某些操作。