gpt4 book ai didi

c++ - 在 Windows Server 2008 rc2 上使用 https 进行 winhttpsendrequest POST

转载 作者:行者123 更新时间:2023-12-03 12:50:43 27 4
gpt4 key购买 nike

我正在编写一个 Visual C++ 应用程序,该应用程序应该将 http post 消息发送到 https 端点(https 服务器的证书是自签名的)。有 2 组客户端,一组在 Windows 7 电脑上,另一组在 Windows Server 2008 rc2 上。使用 mmc 控制台/管理单元(在两个电脑组上)导入服务器的自签名证书后,Windows 7 电脑可以毫无错误地建立连接,但在 Windows Server 2008 rc2 计算机上,winhttpsendrequest 始终返回错误 12030( ) 称呼。有什么想法如何解决这个问题吗?任何帮助/提示将不胜感激。提前致谢。我的代码如下。

  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 观察流量?

要排除身份验证步骤的问题,请尝试传递一些讨论的 SECURITY_FLAG_IGNORE_CERT_* 标志 here .

此外,请考虑使用WinInet APIs而不是 WinHTTP。使用前者执行某些操作更容易一些。

关于c++ - 在 Windows Server 2008 rc2 上使用 https 进行 winhttpsendrequest POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27977386/

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