0

I am writing a visual c++ application that is supposed to send a http post message to an https endpoint (the https server's certificate is self signed). There are 2 groups of clients, 1 on windows 7 pcs and the other group on windows server 2008 rc2. After importing the server's self signed cert using the mmc console/snap-in (on both pc groups), the windows 7 pcs can make the connection without errors but on the Windows server 2008 rc2 machines, the error 12030 is always returned by winhttpsendrequest() call. Any ideas how to resolve this issue? Any help/tip will be appreciated. Thanks in advance. my code is below.

  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());
    }
   }
  }
user3689913
  • 382
  • 4
  • 10

1 Answers1

2

12030 is this:

ERROR_INTERNET_CONNECTION_ABORTED
12030
The connection with the server has been terminated.

That seems like it might be something more than an auth or certificate error.

Have you tried checking your server logs to see how it handled the connection? Observing the traffic with Fiddler?

To rule out the authentication step as a problem, try passing some of the SECURITY_FLAG_IGNORE_CERT_* flags discussed here.

Also, consider using WinInet APIs instead of WinHTTP. A bit easier to do certain ops with the former.

selbie
  • 100,020
  • 15
  • 103
  • 173
  • I guess I'll try the wininet approach and update later... thanks for the suggestion – user3689913 Jan 16 '15 at 08:10
  • Well I tried the wininet approach and the code works on both the windows 7 and windows 2008 rc2 clients.. I guess winhttp just has some issues that i'll need to figure out some other time. Thanks again. – user3689913 Jan 16 '15 at 10:33
  • @user3689913 - you can thank me by upvoting and/or accepting the answer – selbie Jan 16 '15 at 10:55
  • im having the same problem now but i want to use winhttp. Cant receive data it returns `12030`where do I put `SECURITY_FLAG_IGNORE_CERT_`? which function/argument ? https://stackoverflow.com/questions/59730223/receiving-12030-at-winhttpreceiveresponse – Ahmed Can Unbay Jan 14 '20 at 11:28
  • @selbie help me as well please do i need a .cert file or something why doesnt my code work – Ahmed Can Unbay Jan 14 '20 at 11:29
  • @turmuka - I just answered your question. See my answer [here](https://stackoverflow.com/a/59740876/104458) – selbie Jan 14 '20 at 19:56