none
WinHttpSendRequest fails with error code 12002

    Question

  • I am using the below mentioned code to estabilish a connection but WinHttpSendRequest fails everytime.


    DWORD dwSize = 0;

    DWORD dwDownloaded = 0;

    LPSTR pszOutBuffer;

    BOOL bResults = FALSE;

    HINTERNET hSession = NULL,

    hConnect = NULL,

    hRequest = NULL;

     

    // Use WinHttpOpen to obtain a session handle.

    hSession = WinHttpOpen( L

    "WinHTTP Example/1.0",

    WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,

    WINHTTP_NO_PROXY_NAME,

    WINHTTP_NO_PROXY_BYPASS, 0 );

     

    // Specify an HTTP server.

     

    if( hSession )

    hConnect = WinHttpConnect( hSession, L

    "localhost",

    INTERNET_DEFAULT_HTTP_PORT, 0 );

     

    // Create an HTTP request handle.

     

    if( hConnect )

    hRequest = WinHttpOpenRequest( hConnect, L

    "GET", L"/a/test",

    NULL, WINHTTP_NO_REFERER,

    WINHTTP_DEFAULT_ACCEPT_TYPES,

    WINHTTP_FLAG_SECURE );

     

    // Send a request.

     

    if( hRequest )

    bResults = WinHttpSendRequest( hRequest,

    WINHTTP_NO_ADDITIONAL_HEADERS , 0,

    WINHTTP_NO_REQUEST_DATA, 0,

    0, 0 );

    printf(

    "%d", GetLastError());

     

     

    // 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 ) )

    MessageBox(hWnd, _T(

    "Error %u in WinHttpQueryDataAvailable.\n"), NULL,0);

     

    // Allocate space for the buffer.

    pszOutBuffer =

    new char[dwSize+1];

     

    if( !pszOutBuffer )

    {

    MessageBox(hWnd, _T(

    "Out of memory\n"), NULL,0 );

    dwSize=0;

    }

     

    else

    {

     

    // Read the data.

    ZeroMemory( pszOutBuffer, dwSize+1 );

     

    if( !WinHttpReadData( hRequest, (LPVOID)pszOutBuffer,

    dwSize, &dwDownloaded ) )

    MessageBox(hWnd, _T(

    "Error %u in WinHttpReadData.\n"),NULL,0);

     

    // else

     

    // MessageBox(hWnd, pszOutBuffer,NULL,0 );

     

    // Free the memory allocated to the buffer.

     

    delete [] pszOutBuffer;

    }

    }

    while( dwSize > 0 );

    }

     

     

    // Report any errors.

    TCHAR temp[100];

     

    char temp1[100];

    sprintf(temp1,

    "Error = %d", GetLastError());

    mbstowcs(temp, temp1, 100);

     

    if( !bResults )

    MessageBox(hWnd,temp, 0, 0);

     

    // Close any open handles.

     

    if( hRequest ) WinHttpCloseHandle( hRequest );

     

    if( hConnect ) WinHttpCloseHandle( hConnect );

     

    if( hSession ) WinHttpCloseHandle( hSession );

     

     

     WinHttpSendRequest  fails with error 12002..

    Plz help I cant find d reason..
    Thanks in advance
    Tuesday, May 12, 2009 9:57 AM