home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 December / PCWKCD1296.iso / vjplusb / activex / inetsdk / samples / wininet / headdump / headdump.cpp next >
Encoding:
C/C++ Source or Header  |  1996-07-11  |  7.2 KB  |  257 lines

  1. /****************************************************************************
  2. *
  3. * HeadDump: requests the default HTML document from the server and prints 
  4. *            along with HTTP headers.
  5. *            Microsoft Corporation (C). Leon Braginski.
  6. *    
  7. * PURPOSE: 
  8. * This sample demonstrates how to create and submit HTTP 
  9. * request. Sample requests the default HTML document from the
  10. * server and then display it along with the HTTP transaction headers.
  11. * This sample also allows to access password protected pages. It 
  12. * checks for HTTP server response code and it is "401 Access Denied"
  13. * it asks password and user name and then resubmit request.
  14. * Note that is only works with the Basic authentication scheme, since 
  15. * this scheme is built into WinInet APIs. Please see documentation for
  16. * WinInet APIs for more details.
  17. *
  18. * COMMENTS: 
  19. * This sample can either work with the direct access to the Internet or
  20. * via proxy server.
  21. * USAGE:
  22. * With direct connection to the Internet:
  23. *    C:> headdump.exe www.server.name
  24. * Via proxy server:
  25. *    C:> headdump.exe www.server.name http://MyProxyServer:ProxyPort
  26. *
  27. * Note that sample continuously output information on the screen, so use like
  28. * this:
  29. *     C:> headdump.exe www.server.name | more
  30. *
  31. * This sample has been tested under Windows NT 4.0 Beta and Windows 95.
  32. ****************************************************************************/
  33.  
  34.  
  35.  
  36. #include <windows.h>
  37. #include <wininet.h>
  38. #include <iostream.h>
  39.  
  40. BOOL ErrorOut  ( DWORD dError, TCHAR * szCallingFunc);
  41.  
  42. int main (int argc, char *argv[])
  43. {
  44.  HINTERNET hOpen, hConnect, hReq;
  45.  DWORD  dwSize, dwCode;
  46.  CHAR *lpBuffer, szData[51];
  47.  
  48.  
  49. switch (argc)
  50. {
  51.     case 1:
  52.     {
  53.         cerr << "Usage: " << argv[0] <<" host [proxy]" << endl;
  54.         cerr << "\twhere host is a HTTP server such as www.server.com" << endl;
  55.         cerr << "\tand proxy is optional proxy in form: http://proxy:80" << endl;
  56.         return 0;
  57.     }
  58.     case 2:
  59.     {
  60.         if ( !(hOpen = InternetOpen ( "HeadDump",  LOCAL_INTERNET_ACCESS , NULL, 0, 0) ) )
  61.         {
  62.             ErrorOut ( GetLastError(), "InternetOpen");
  63.             return 0;
  64.         }
  65.         break;
  66.     }
  67.     case 3:
  68.     {
  69.        if ( !(hOpen = InternetOpen ( "HeadDump",  CERN_PROXY_INTERNET_ACCESS, argv[2], NULL, 0) ) )
  70.        {
  71.             ErrorOut ( GetLastError(), "InternetOpen");
  72.            return 0;
  73.        }
  74.        break;
  75.     }
  76.  }   
  77.         
  78.  
  79. if ( !(hConnect = InternetConnect ( hOpen, argv[1] , INTERNET_INVALID_PORT_NUMBER, "",  "", INTERNET_SERVICE_HTTP, 0  , 0) ) )
  80. {
  81.     ErrorOut (GetLastError(), "InternetConnect");
  82.     return 0;
  83. }
  84.  
  85. if ( !(hReq = HttpOpenRequest (hConnect, "GET", "", HTTP_VERSION, "", NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE ,0 )))
  86. {
  87.     ErrorOut (GetLastError(), "HttpOpenRequest");
  88.     return 0;
  89. }
  90.  
  91.  
  92. again:
  93. if (!HttpSendRequest (hReq, NULL, 0, NULL, 0) )
  94. {
  95.     ErrorOut (GetLastError(), "HttpSend");
  96.     return 0;
  97. }
  98.  
  99. dwSize = sizeof (DWORD) ;  
  100. if ( !HttpQueryInfo (hReq, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwCode, &dwSize, NULL))
  101. {
  102.     ErrorOut (GetLastError(), "HttpQueryInfo");
  103.     return FALSE;
  104. }
  105.  
  106. if ( dwCode == HTTP_STATUS_DENIED || dwCode == HTTP_STATUS_PROXY_AUTH_REQ)
  107. {  
  108.     // This is a secure page.
  109.     cerr << "This page is password protected. " << endl;
  110.     CHAR szUser[50]="";
  111.     CHAR szPass[50]="";
  112.  
  113.     cerr << "User: ";
  114.     cin  >> szUser;
  115.     cerr << "Pass: ";
  116.     cin  >> szPass;
  117.  
  118.  
  119.     // We have to read all outstanding data on the Internet handle
  120.     // before we can resubmit request. Just discard the data.
  121.     do
  122.     {
  123.         InternetReadFile (hReq, (LPVOID)szData, 50, &dwSize);
  124.     }
  125.     while (dwSize != 0);
  126.  
  127.  
  128.     if ( !InternetSetOption (hConnect, INTERNET_OPTION_USERNAME, (LPVOID) szUser, lstrlen (szUser) ))
  129.     {
  130.         cerr << "InternetSetOptionFailed: " << GetLastError() << endl;
  131.         return FALSE;
  132.     }
  133.     
  134.     if ( !InternetSetOption (hConnect, INTERNET_OPTION_PASSWORD, (LPVOID) szPass, lstrlen (szPass) ))
  135.     {
  136.         cerr << "InternetSetOptionFailed: " << GetLastError() << endl;
  137.         return FALSE;
  138.     }
  139.     goto again;
  140. }
  141.  
  142.  
  143.  
  144.  
  145. // First time we will find out the size of the headers.
  146. HttpQueryInfo (hReq,HTTP_QUERY_RAW_HEADERS_CRLF,  NULL, &dwSize, NULL);
  147. lpBuffer =  new char [dwSize + 1 ];
  148.  
  149. // Now we call HttpQueryInfo again to get the headers.
  150. if (!HttpQueryInfo (hReq,HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID) lpBuffer,
  151.     &dwSize, NULL))
  152. {
  153.     ErrorOut (GetLastError(), "HttpQueryInfo");
  154.     return FALSE;
  155. }
  156. *(lpBuffer + dwSize) = '\0';
  157. cout << lpBuffer << endl;
  158.  
  159. // We can relay on the CONTENTS_LENGTH header to find the size of the 
  160. // HTML file, since this header may not exist (it is optional for HTTP/1.0
  161. do
  162. {
  163.     if (!InternetReadFile (hReq, (LPVOID)szData, 50, &dwSize) )
  164.     {
  165.          ErrorOut (GetLastError (), "InternetReadFile");
  166.         return FALSE;
  167.     }
  168.     if ( !dwSize)
  169.         break;
  170.     else
  171.     {
  172.         szData [dwSize] = '\0'; 
  173.         cout << szData;
  174.     }
  175. }
  176. while (TRUE);
  177. cout << endl;
  178.  
  179. if (!InternetCloseHandle (hReq) )
  180. {
  181.     ErrorOut (GetLastError (), "CloseHandle on hReq");
  182.     return FALSE;
  183. }
  184. if (!InternetCloseHandle (hConnect) )
  185. {
  186.     ErrorOut (GetLastError (), "CloseHandle on hConnect");
  187.     return FALSE;
  188. }
  189. if (!InternetCloseHandle (hOpen) )
  190. {
  191.     ErrorOut (GetLastError (), "CloseHandle on hOpen");
  192.     return FALSE;
  193. }
  194.  
  195.  
  196. free (lpBuffer);
  197. return TRUE;
  198. }
  199.  
  200. /****************************************************************************
  201. *
  202. *    FUNCTION: ErrorOut
  203. *
  204. *    PURPOSE: This function is used to get extended Internet error.
  205. *
  206. *    COMMENTS:  Function returns TRUE on success and FALSE on failure.
  207. *
  208. ****************************************************************************/
  209.  
  210. BOOL ErrorOut ( DWORD dError, TCHAR * szCallFunc)
  211. {
  212.     TCHAR szTemp[100] = "", *szBuffer=NULL, *szBufferFinal = NULL;
  213.     DWORD  dwIntError , dwLength = 0; 
  214.     wsprintf (szTemp,  "%s error %d\n ", szCallFunc, dError );
  215.     if (dError == ERROR_INTERNET_EXTENDED_ERROR)
  216.     {
  217.         InternetGetLastResponseInfo (&dwIntError, NULL, &dwLength);
  218.         if (dwLength)
  219.         {
  220.             if ( !(szBuffer = (TCHAR *) LocalAlloc ( LPTR,  dwLength) ) )
  221.             {
  222.                 lstrcat (szTemp, TEXT ( "Unable to allocate memory to display Internet error code. Error code: ") );
  223.                 lstrcat (szTemp, TEXT (_itoa (GetLastError(), szBuffer, 10)  ) );
  224.                 lstrcat (szTemp, TEXT ("\n") );
  225.                 cerr << szTemp << endl;
  226.                 return FALSE;
  227.             }
  228.             if (!InternetGetLastResponseInfo (&dwIntError, (LPTSTR) szBuffer, &dwLength))
  229.             {
  230.                 lstrcat (szTemp, TEXT ( "Unable to get Intrnet error. Error code: ") );
  231.                 lstrcat (szTemp, TEXT (_itoa (GetLastError(), szBuffer, 10)  ) );
  232.                 lstrcat (szTemp, TEXT ("\n") );
  233.                 cerr << szTemp << endl;
  234.                 return FALSE;
  235.             }
  236.             if ( !(szBufferFinal = (TCHAR *) LocalAlloc ( LPTR,  (strlen (szBuffer) +strlen (szTemp) + 1)  ) )  )
  237.             {
  238.                 lstrcat (szTemp, TEXT ( "Unable to allocate memory. Error code: ") );
  239.                 lstrcat (szTemp, TEXT (_itoa (GetLastError(), szBuffer, 10)  ) );
  240.                 lstrcat (szTemp, TEXT ("\n") );
  241.                 cerr << szTemp << endl;
  242.                 return FALSE;
  243.             }
  244.             lstrcpy (szBufferFinal, szTemp);
  245.             lstrcat (szBufferFinal, szBuffer);
  246.             LocalFree (szBuffer);
  247.             cerr <<  szBufferFinal  << endl;
  248.             LocalFree (szBufferFinal);
  249.         }
  250.     }
  251.     else
  252.         cerr << szTemp << endl;
  253.     return TRUE;
  254. }
  255.