WinHttp problems

This is a discussion on WinHttp problems within the Windows Programming forums, part of the Platform Specific Boards category; Hi Im currently trying to write a program that will connect and login to site with http. When logged in, ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    WinHttp problems

    Hi
    Im currently trying to write a program that will connect and login to site with http. When logged in, I want to access xml-data at the site.
    I've decided to use WinHttp, but I not quite sure what to do to make it work.

    The site I want to access is www.hattrick.org. Login procedure:
    http://195.149.159.158/Common/defaul...sword=PASSWORD
    then then you can access for example:
    http://195.149.159.158/Common/league...eLevelUnitID=1

    code:
    **********
    hSession = WinHttpOpen( L"A WinHTTP Example Program/1.0",
    WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
    WINHTTP_NO_PROXY_NAME,
    WINHTTP_NO_PROXY_BYPASS, 0 );
    hConnect = WinHttpConnect( hSession,
    L"www.hattrick.org",
    INTERNET_DEFAULT_HTTP_PORT,
    0 );
    hRequest = WinHttpOpenRequest( hConnect,
    L"GET",
    L"http://195.149.159.158/Common/default.asp?action=login&loginName=USERNAME&passwo rd=PASSWORD",
    NULL,
    WINHTTP_NO_REFERER,
    WINHTTP_DEFAULT_ACCEPT_TYPES,
    0 );
    **********
    I dont know what to do next. I want to access the 'other' sites to gater xmldata, but I dont know how to do it.
    I am even not sure if Im logged in, I have used GetLastError() but it returns no error code.

    Any suggestions?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    >> Any suggestions?

    Why don't you start by reading the RFC's on the protocol?

    >> I want to access the 'other' sites to gater xmldata...

    What 'other' sites are you referring to?

    >> I am even not sure if Im logged in...

    GetLastError() doesn't do anything for you. Try testing your 'hConnect' handle.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    MSDN provides complete sample code for downloading a resource here.

    The sample code is for downloading a resource using ssl(https), so remember to change the flags in WinHttpConnect and WinHttpOpenRequest if you do not want ssl.

    Following is the sample code with highlighted changes to download the resource you specify.
    Code:
      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"195.149.159.158",
                                   INTERNET_DEFAULT_HTTP_PORT, 0 );
    
      // Create an HTTP request handle.
      if( hConnect )
        hRequest = WinHttpOpenRequest( hConnect, L"GET", L"/Common/default.asp?action=login&loginName=USERNAME&password=PASSWORD",
                                       NULL, WINHTTP_NO_REFERER, 
                                       WINHTTP_DEFAULT_ACCEPT_TYPES, 
                                       0 );
    
      // Send a request.
      if( hRequest )
        bResults = WinHttpSendRequest( hRequest,
                                       WINHTTP_NO_ADDITIONAL_HEADERS, 0,
                                       WINHTTP_NO_REQUEST_DATA, 0, 
                                       0, 0 );
    
    
      // 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 ) )
            printf( "Error %u in WinHttpQueryDataAvailable.\n",
                    GetLastError( ) );
    
          // Allocate space for the buffer.
          pszOutBuffer = new char[dwSize+1];
          if( !pszOutBuffer )
          {
            printf( "Out of memory\n" );
            dwSize=0;
          }
          else
          {
            // Read the data.
            ZeroMemory( pszOutBuffer, dwSize+1 );
    
            if( !WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, 
                                  dwSize, &dwDownloaded ) )
              printf( "Error %u in WinHttpReadData.\n", GetLastError( ) );
            else
              printf( "%s", pszOutBuffer );
    
            // Free the memory allocated to the buffer.
            delete [] pszOutBuffer;
          }
        } while( dwSize > 0 );
      }
    
    
      // Report any errors.
      if( !bResults )
        printf( "Error %d has occurred.\n", GetLastError( ) );
    
      // Close any open handles.
      if( hRequest ) WinHttpCloseHandle( hRequest );
      if( hConnect ) WinHttpCloseHandle( hConnect );
      if( hSession ) WinHttpCloseHandle( hSession );

    >> want to access the 'other' sites to gater xmldata, but I dont know how to do it.<<

    Once you have "logged in" with the above page, your credentials will be stored in a cookie and you will be able to download other pages with similar code.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    Thanks!

    I have a few additional questions about the code:

    * Is the login procedure all the pasted code, or just the WinHttpOpenRequest() and WinHttpSendRequest()?

    >>Once you have "logged in" with the above page, your >>credentials will be stored in a cookie and you will be able to >>download other pages with similar code.

    * If I've understand this correct, you can for example use WinHttpOpenRequest() with "/Common/leagueDetails.asp?outputType=XML&actionType=view&l eagueLevelUnitID=1" to access other 'sites'?
    And afterwards, you use WinHttpSendRequest, WinHttpReceiveResponse, WinHttpQueryDataAvailable and WinHttpReadData to get the cookie data?

    PS: Sry for the newbie questions, but I've spent hour after hour reading the msdn without doing much of progress in my programming :/

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >>Is the login procedure all the pasted code, or just the WinHttpOpenRequest() and WinHttpSendRequest()?<<

    Remember, the login is only another web page. The pasted code is what is required to download a web page and print it to output.

    You may be able to get rid of the code after "// Keep checking for data". This would be the equivalent of pressing the stop button in your browser before the page was loaded.

    If you want to load a different page pass its location to WinHttpOpenRequest instead of
    "/Common/default.asp?action=login&loginName=USERNAME&passwo rd=PASSWORD".

    >>And afterwards, you use WinHttpSendRequest, WinHttpReceiveResponse, WinHttpQueryDataAvailable and WinHttpReadData to get the cookie data?<<

    WinHttp manages the cookies for you. You do not have to do anything.

    When you load the login page the site will send back a cookie with your identity. WinHttp saves this cookie for later.

    When you visit another page at the same site(195.149.159.158) WinHttp automatically sends the cookie back to the server. This allows the site to verify your identity that it set on the login page.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    anonytmouse: Thank you!
    I works just fine now!

    And whats even more important, I think I understand it! =D

  7. #7
    Registered User
    Join Date
    Feb 2006
    Location
    Leiden, NL
    Posts
    12

    Linux

    Hi there, is a similar method available under linux? Since the winhttp.h is available for Windows???

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    A so beginneth another lesson on reading the rules before deciding to bump threads.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21