Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: None  |  size: 2.92 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // wininet-test.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <windows.h>
  6. #include <wininet.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string>
  10.  
  11. #pragma comment(lib, "wininet.lib")
  12.  
  13. int main()
  14. {
  15.         LPCTSTR lpszAgent = "WinInetGet/0.1";
  16.         HINTERNET hInternet = InternetOpen(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
  17.  
  18.         LPCTSTR lpszServerName = "www.bing.com";
  19.         INTERNET_PORT nServerPort = INTERNET_DEFAULT_HTTP_PORT;
  20.         LPCTSTR lpszUserName = NULL;
  21.         LPCTSTR lpszPassword = NULL;
  22.         DWORD dwConnectFlags = 0;
  23.         DWORD dwConnectContext = 0;
  24.  
  25.         HINTERNET hConnect = InternetConnect(hInternet,lpszServerName, nServerPort,lpszUserName, lpszPassword, INTERNET_SERVICE_HTTP, dwConnectFlags, dwConnectContext);
  26.  
  27.         LPCTSTR lpszVerb = "GET";
  28.         LPCTSTR lpszObjectName = "/search?q=help&first=11";
  29.         LPCTSTR lpszVersion = NULL;                     // Use default.
  30.         LPCTSTR lpszReferrer = NULL;            // No referrer.
  31.         LPCTSTR *lplpszAcceptTypes = NULL;      // Whatever the server wants to give us.
  32.         DWORD dwOpenRequestFlags =      INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS |INTERNET_FLAG_KEEP_CONNECTION |
  33.                                                                 INTERNET_FLAG_NO_AUTH |INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_NO_COOKIES |INTERNET_FLAG_NO_UI |INTERNET_FLAG_RELOAD;
  34.  
  35.         DWORD dwOpenRequestContext = 0;
  36.         HINTERNET hRequest = HttpOpenRequest(hConnect, lpszVerb, lpszObjectName, lpszVersion, lpszReferrer, lplpszAcceptTypes, dwOpenRequestFlags, dwOpenRequestContext);
  37.  
  38.         BOOL bResult = HttpSendRequest(hRequest, NULL, 0, NULL, 0);
  39.         DWORD dwInfoLevel = HTTP_QUERY_RAW_HEADERS_CRLF;
  40.         DWORD dwInfoBufferLength = 10;
  41.         BYTE *pInfoBuffer = (BYTE *)malloc(dwInfoBufferLength+1);
  42.  
  43.         while (!HttpQueryInfo(hRequest, dwInfoLevel, pInfoBuffer, &dwInfoBufferLength, NULL))
  44.         {
  45.                 DWORD dwError = GetLastError();
  46.                 if (dwError == ERROR_INSUFFICIENT_BUFFER)
  47.                 {
  48.                         free(pInfoBuffer);
  49.                         pInfoBuffer = (BYTE *)malloc(dwInfoBufferLength+1);
  50.                 }
  51.                 else
  52.                 {
  53.                         fprintf(stderr, "HttpQueryInfo failed, error = %d (0x%x)\n",
  54.                         GetLastError(), GetLastError());
  55.                         break;
  56.                 }
  57.         }
  58.  
  59.         pInfoBuffer[dwInfoBufferLength] = '\0';
  60.         printf("%s", pInfoBuffer);
  61.         free(pInfoBuffer);
  62.  
  63.         DWORD dwBytesAvailable;
  64.  
  65.         while (InternetQueryDataAvailable(hRequest, &dwBytesAvailable, 0, 0))
  66.         {
  67.                 BYTE *pMessageBody = (BYTE *)malloc(dwBytesAvailable+1);
  68.                 DWORD dwBytesRead;
  69.                 BOOL bResult = InternetReadFile(hRequest, pMessageBody, dwBytesAvailable, &dwBytesRead);
  70.                 if (!bResult)
  71.                 {
  72.                         fprintf(stderr, "InternetReadFile failed, error = %d (0x%x)\n",
  73.                         GetLastError(), GetLastError());
  74.                         break;
  75.                 }
  76.  
  77.                 if (dwBytesRead == 0)
  78.                 break;  // End of File.
  79.  
  80.                 pMessageBody[dwBytesRead] = '\0';
  81.                 printf("%s", pMessageBody);
  82.  
  83.                 ///////// OUTPUT HTML ////////
  84.                 FILE *f;
  85.                 f = fopen("site.html","a+");
  86.                 fprintf(f, "%s\n", pMessageBody);
  87.                 fclose(f);
  88.                 free(pMessageBody);
  89.                 //////////////////////////////
  90.         }
  91.         getchar();
  92.         return 0;
  93. }