winhttpsendrequest POST with https on windows server 2008 rc



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

  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 infon");
        printf("access type: %ldn",ProxyInfo.dwAccessType);
        printf("list: %sn",ProxyInfo.lpszProxy);
        printf("bypass list: %sn",ProxyInfo.lpszProxyBypass);
        if( !WinHttpSetOption(  hRequest, 
                                WINHTTP_OPTION_PROXY,
                                &ProxyInfo,
                                cbProxyInfoSize ) )
        {
            printf("Proxy detection logic failedn");
            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 memoryn" );
      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.

这似乎不仅仅是身份验证或证书错误。

您是否尝试过检查服务器日志以查看它如何处理连接? 和小提琴手一起观察交通?

若要排除身份验证步骤作为问题,请尝试传递此处讨论的一些 SECURITY_FLAG_IGNORE_CERT_* 标志。

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

最新更新