// JohnHttpClient.cpp: implementation of the CJohnHttpClient class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <stdio.h>
#include "JohnHttpClient.h"
#include "log.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#pragma comment(lib,"wininet.lib")
#define AGENT_NAME "JOHN WEB BROWSER 1.0"
/**********************************************/
// Http Communication Class.
// Support GET/POST Mehtod
// Author: John Zhao
// Date: 4/8/2008 14:58
// Version:v1.0.0
// Change Log:
/**********************************************/
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CJohnHttpClient::CJohnHttpClient()
{
m_hSession = ::InternetOpen(AGENT_NAME,
INTERNET_OPEN_TYPE_PRECONFIG, // Use registry settings.
NULL, // Proxy name. NULL indicates use default.
NULL, // List of local servers. NULL indicates default.
0) ;
m_hConnection = NULL;
}
CJohnHttpClient::~CJohnHttpClient()
{
Close();
}
BOOL CJohnHttpClient::GetWebPage(LPCTSTR strURL, LPTSTR& strResult)
{
if (m_hSession == NULL || m_hConnection == NULL) return FALSE;
HINTERNET hHttpFile = HttpOpenRequest(m_hConnection,
TEXT("GET"), strURL,
NULL, NULL, NULL,
INTERNET_FLAG_KEEP_CONNECTION,
0 );
HttpSendRequest(hHttpFile, NULL, 0, NULL, 0);
return Read(hHttpFile,strResult);
}
BOOL CJohnHttpClient::Close()
{
if (m_hSession)
{
InternetCloseHandle(m_hSession);
m_hSession = NULL;
}
if (m_hConnection)
{
InternetCloseHandle(m_hConnection);
m_hConnection = NULL;
}
return TRUE;
}
BOOL CJohnHttpClient::Read(HINTERNET hHttpFile, LPTSTR &strResult)
{
BOOL bResult = FALSE;
char szSizeBuffer[32];
DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer);
DWORD dwFileSize;
DWORD dwBytesRead;
try
{
if (hHttpFile)
{
// Getting the size of HTTP Files
BOOL bQuery = ::HttpQueryInfo(hHttpFile,HTTP_QUERY_CONTENT_LENGTH,
szSizeBuffer, &dwLengthSizeBuffer, NULL) ;
BOOL bRead;
if(bQuery==TRUE)
{
// Allocating the memory space for HTTP file contents
dwFileSize=atol(szSizeBuffer);
//LPSTR szContents = Contents.GetBuffer(dwFileSize);
strResult = (TCHAR*)malloc(dwFileSize+1);
memset(strResult,0,sizeof(strResult));
// Read the HTTP file
DWORD dwPos = 0;
do {
Sleep(0);
bRead = ::InternetReadFile(hHttpFile, strResult+dwPos, dwFileSize-dwPos, &dwBytesRead);
dwPos += dwBytesRead;
} while (bRead && dwBytesRead>0);
}
else
{
// setting static buffer size, as we cannot determine it
#define BUF_SIZE 50000
char szBuffer[BUF_SIZE+1];
strResult = (TCHAR*)malloc(BUF_SIZE+1);
memset(strResult,0,sizeof(strResult));
int iBuffSize = BUF_SIZE+1;
// Read the HTTP file
BOOL bRead;
DWORD dwPos = 0;
do {
Sleep(0);
bRead = ::InternetReadFile(hHttpFile, szBuffer, BUF_SIZE, &dwBytesRead);
dwPos += dwBytesRead;
// ensuring, that buffer is dwBytesRead size
if ( dwBytesRead > 0 )
{
szBuffer[dwBytesRead] = '\0';
if (dwPos>=iBuffSize)
{
iBuffSize+=30000;
strResult = (TCHAR*)realloc(strResult,iBuffSize);
}
strcat(strResult,szBuffer);
}
} while (bRead && dwBytesRead>0);
}
if (bRead)
bResult = TRUE;
::InternetCloseHandle(hHttpFile); // Close the connection.
}
}
catch (CException* ex)
{
CLog::LogSQL("出错BOOL CJohnHttpClient::Read(HINTERNET hHttpFile, LPTSTR &strResult)");
}
return bResult;
}
BOOL CJohnHttpClient::PostWebPage(LPCTSTR strURL,LPTSTR &strResult)
{
if (m_hSession == NULL || m_hConnection == NULL) return FALSE;
HINTERNET hHttpFile = HttpOpenRequest(
m_hConnection,
TEXT("POST"), strURL,
NULL, NULL, NULL,
INTERNET_FLAG_KEEP_CONNECTION,
0 );
HttpSendRequest(hHttpFile, NULL, 0, NULL, 0);
if (!hHttpFile) return FALSE;
return Read(hHttpFile,strResult);
}
BOOL CJohnHttpClient::SingleModeGetWebPage(LPCTSTR strURL, LPTSTR& strResult)
{
if (m_hSession == NULL) return FALSE;
HINTERNET hHttpFile = ::InternetOpenUrl(m_hSession,strURL,NULL
,NULL,
INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_MAKE_PERSISTENT
|INTERNET_FLAG_READ_PREFETCH
,NULL);
return Read(hHttpFile,strResult);
}
BOOL CJohnHttpClient::InitConnect(LPCTSTR strServer, int iPort)
{
if (m_hConnection != NULL)
{
InternetCloseHandle(m_hConnection);
m_hConnection = NULL;
}
if (m_hSession)
{
m_hConnection = ::InternetConnect(m_hSession,
strServer,
iPort,
" "," ", INTERNET_SERVICE_HTTP, 0, 0);
if ( !m_hConnection )
{
InternetCloseHandle(m_hSession);
m_hSession = NULL;
memset(m_strMSG,0,sizeof(m_strMSG));
sprintf(m_strMSG,TEXT("Couldn't connect %s through port %d."),strServer,iPort);
return FALSE;
}
return TRUE;
}
else
{
memset(m_strMSG,0,sizeof(m_strMSG));
sprintf(m_strMSG,TEXT("Session is NULL."));
return FALSE;
}
}
BOOL CJohnHttpClient::GetError(LPTSTR strRMSG)
{
memcpy(strRMSG,m_strMSG,strlen(m_strMSG)+1);
return TRUE;
}
BOOL CJohnHttpClient::GetWebPage(const CString &Url, CString &resultString)
{
HINTERNET hHttpFile;
char szSizeBuffer[32];
DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer);
DWORD dwFileSize;
DWORD dwBytesRead;
BOOL bSuccessful;
// Setting default error message
resultString = _T("");
try
{
// Opening the Url and getting a Handle for HTTP file
hHttpFile = InternetOpenUrl(m_hSession, (const char *) Url, NULL, 0, 0, 0);
if (hHttpFile)
{
// Getting the size of HTTP Files
BOOL bQuery = ::HttpQueryInfo(hHttpFile,HTTP_QUERY_CONTENT_LENGTH,
szSizeBuffer, &dwLengthSizeBuffer, NULL) ;
BOOL bRead;
if(bQuery==TRUE)
{
// Allocating the memory space for HTTP file resultString
dwFileSize=atol(szSizeBuffer);
LPSTR szResultString = resultString.GetBuffer(dwFileSize);
// Read the HTTP file
DWORD dwPos = 0;
do {
Sleep(0);
bRead = ::InternetReadFile(hHttpFile, szResultString+dwPos, dwFileSize-dwPos, &dwBytesRead);
dwPos += dwBytesRead;
} while (bRead && dwBytesRead>0);
resultString.ReleaseBuffer(dwPos);
}
else
{
// setting static buffer size, as we cannot determine it
#define BUF_SIZE 50000
char szBuffer[BUF_SIZE+1];
resultString.Empty();
// Read the HTTP file
BOOL bRead;
DWORD dwPos = 0;
do {
Sleep(0);
bRead = ::InternetReadFile(hHttpFile, szBuffer, BUF_SIZE, &dwBytesRead);
dwPos += dwBytesRead;
// ensuring, that buffer is dwBytesRead size
if ( dwBytesRead > 0 )
{
szBuffer[dwBytesRead] = '\0';
resultString += szBuffer;
}
} while (bRead && dwBytesRead>0);
}
//if (bRead)
bSuccessful = TRUE;
::InternetCloseHandle(hHttpFile); // Close the connection.
}
else
{
// Connection failed.
bSuccessful = FALSE;
}
}
catch (CException* ex)
{
CLog::LogSQL("出错 CJohnHttpClient::GetWebPage(const CString &Url, CString &resultString)");
}
return bSuccessful;
}
//Download Picture from the internet
BOOL CJohnHttpClient::GetPicture(LPCTSTR strURL,LPTSTR& pResultBuff,DWORD& dwRetBytesRead)
{
BOOL bResult = FALSE;
char szSizeBuffer[32];
DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer);
DWORD dwFileSize;
DWORD dwBytesRead;
try
{
HINTERNET hHttpFile = InternetOpenUrl(m_hSession, strURL, NULL, 0, 0, 0);
if (hHttpFile)
{
// Getting the