Connect internet with HINTERNET


HTTP.h

   1: #pragma once
   2: #include "Wininet.h"
   3: class HTTP
   4: {
   5: private:
   6:     HINTERNET hInstance, hConnect, hRequest;
   7:     DWORD hSendRequest;
   8:     CString servername,lastoutput;
   9:
  10: public:
  11:     HTTP(void);
  12:     bool Connect(CString servername);
  13:     bool SentGet(CString pszPageName);
  14:     void GetLastOutput(CString &lastOutput);
  15:     ~HTTP(void);
  16: protected:
  17:     void ErrorText(LPTSTR pszFunction);
  18: };
  19:

HTTP.cpp

   1: #include "StdAfx.h"
   2: #include "HTTP.h"
   3:
   4:
   5: HTTP::HTTP(void)
   6: {
   7:     this->hInstance = InternetOpen(L"Mozilla//5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.8) Gecko//2009032609 Firefox//3.0.8 (.NET CLR 3.5.30729)",
   8:                                        INTERNET_OPEN_TYPE_PRECONFIG,
   9:                                        NULL,
  10:                                        NULL,
  11:                                        NULL);
  12:
  13: }
  14:
  15: bool HTTP::Connect(CString servername)
  16: {
  17:     this->hConnect = InternetConnect(this->hInstance,
  18:                            servername.GetString(),
  19:                            INTERNET_DEFAULT_HTTP_PORT,
  20:                            NULL,
  21:                            NULL,
  22:                            INTERNET_SERVICE_HTTP,
  23:                            0,
  24:                            1);
  25:     if (hConnect == NULL)
  26:     {
  27:         ErrorText(TEXT("InternetConnect"));
  28:         return false;
  29:     }
  30:     this->servername=servername;
  31:     return true;
  32: }
  33: bool HTTP::SentGet(CString pszPageName)
  34: {
  35:     this->hRequest = HttpOpenRequest(this->hConnect,
  36:                            L"GET",
  37:                            pszPageName.GetString(),
  38:                            NULL,
  39:                            NULL,
  40:                            NULL,
  41:                            INTERNET_FLAG_RELOAD | INTERNET_FLAG_EXISTING_CONNECT,
  42:                            0);
  43:     if (hRequest == NULL)
  44:     {
  45:         ErrorText(TEXT("HttpOpenRequest"));
  46:         return false;
  47:     }
  48:     hSendRequest = HttpSendRequest(hRequest,
  49:                         NULL,
  50:                         0,
  51:                         NULL,
  52:                         0);
  53:     if (hSendRequest == NULL)
  54:     {
  55:         ErrorText(TEXT("HttpSendRequest"));
  56:         return false;
  57:     }
  58:
  59:     BOOL bRead=TRUE;
  60:     CHAR szBuffer[2048];
  61:     DWORD dwNumberOfBytesToRead = 2048;
  62:     DWORD lpNumberOfBytesRead;
  63:     CString buffer(L"");
  64:     CString outputGet(L"");
  65:
  66:     while(InternetReadFile(hRequest,szBuffer,dwNumberOfBytesToRead,&lpNumberOfBytesRead))
  67:     {
  68:         if( lpNumberOfBytesRead== 0)
  69:             break;
  70:
  71:         //std::cout<<(char *)(LPCTSTR)szBuffer;
  72:         buffer=szBuffer;
  73:         outputGet+=buffer.Left(lpNumberOfBytesRead);
  74:     }
  75:     this->lastoutput=outputGet;
  76:     return true;
  77: }
  78: void HTTP::GetLastOutput(CString &lastOutput)
  79: {
  80:     lastOutput=this->lastoutput;
  81: }
  82: void HTTP::ErrorText(LPTSTR pszFunction)
  83: {
  84:     // Retrieve the system error message for the last-error code
  85:     // Using : ErrorText(TEXT("HttpSendRequest"));
  86:     LPTSTR pszMessage;
  87:     DWORD dwLastError = GetLastError();
  88:
  89:     FormatMessage(
  90:         FORMAT_MESSAGE_ALLOCATE_BUFFER |
  91:         FORMAT_MESSAGE_FROM_SYSTEM |
  92:         FORMAT_MESSAGE_IGNORE_INSERTS,
  93:         NULL,
  94:         dwLastError,
  95:         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  96:         (LPTSTR)&pszMessage,
  97:         0, NULL );
  98:
  99:     // Display the error message and exit the process
 100:     wprintf(L"%s Error %d: %s", pszFunction, dwLastError, pszMessage);
 101:
 102:     LocalFree(pszMessage);
 103:     //system("pause");
 104:     //ExitProcess(dwLastError); 
 105: }
 106: HTTP::~HTTP(void)
 107: {
 108:      InternetCloseHandle(this->hConnect);
 109: }

TestClass.cpp

   1: #include "stdafx.h"
   2: #include "HTTP.h"
   3:
   4: using namespace std;
   5:
   6: int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
   7: {
   8:     int nRetCode = 0;
   9:     CString oatstr;
  10:     HTTP OAT;
  11:     OAT.Connect(L"www.google.com");
  12:     OAT.SentGet(L"/index.php");
  13:     OAT.GetLastOutput(oatstr);
  14:     std::wcout<<oatstr.GetString()<<endl;
  15:     std::wcout<<"======================================="<<endl;
  16:     OAT.Connect(L"www.thaidev.com");
  17:     OAT.SentGet(L"/index.php");
  18:     OAT.GetLastOutput(oatstr);
  19:     std::wcout<<oatstr.GetString()<<endl;
  20:     system("pause");
  21:     return nRetCode;
  22: }

 

OUTPUT

ติดป้ายกำกับ:

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out / เปลี่ยนแปลง )

Connecting to %s

%d bloggers like this: