Click here to Skip to main content
12,752,054 members (31,723 online)
Click here to Skip to main content
Add your own
alternative version

Stats

457.7K views
11.7K downloads
141 bookmarked
Posted 23 Mar 2003

Simple HTTP Client using WININET

, 2 Sep 2003
Rate this:
Please Sign up or sign in to vote.
Simple HTTP Client, HTTP GET, HTTP POST, HTTP POST-MultiPartFormData

Introduction

This class is used for HTTP REQUEST. It supports HTTP GET, HTTP POST and HTTP POST-MultiPartFormData.

Class Overview

<PRE lang=c++>class GenericHTTPClient { public: ... // CONSTRUCTOR & DESTRUCTOR GenericHTTPClient(); virtual ~GenericHTTPClient(); ... // Connection handler BOOL Connect( LPCTSTR szAddress, LPCTSTR szAgent = __DEFAULT_AGENT_NAME, unsigned short nPort = INTERNET_DEFAULT_HTTP_PORT, LPCTSTR szUserAccount = NULL, LPCTSTR szPassword = NULL); BOOL Close(); VOID InitilizePostArguments(); // HTTP Arguments handler VOID AddPostArguments(LPCTSTR szName, DWORD nValue); VOID AddPostArguments(LPCTSTR szName, LPCTSTR szValue, BOOL bBinary = FALSE); // HTTP Method handler BOOL Request( LPCTSTR szURL, int nMethod = GenericHTTPClient::RequestGetMethod, LPCTSTR szAgent = __DEFAULT_AGENT_NAME); BOOL RequestOfURI(LPCTSTR szURI, int nMethod = GenericHTTPClient::RequestGetMethod); BOOL Response(PBYTE pHeaderBuffer, DWORD dwHeaderBufferLength, PBYTE pBuffer, DWORD dwBufferLength, DWORD &dwResultSize); LPCTSTR QueryHTTPResponse(); LPCTSTR QueryHTTPResponseHeader(); // General Handler DWORD GetLastError(); LPCTSTR GetContentType(LPCTSTR szName); VOID ParseURL(LPCTSTR szURL, LPTSTR szProtocol, LPTSTR szAddress, DWORD &dwPort, LPTSTR szURI); protected: ... };
  • Connect(...) method connects to HTTP Server.
  • Close() method closes connection. These are used with RequestOfURI(...).
  • InitilizePostArguments() method initializes post arguments.
  • AddPostArguments(...) method is supported so that you can add new post arguments of the following 3 types ( TCHAR, DWORD, FILE).
  • Request(...) method is for you to attempt request for HTTP REQUEST( GET, POST, POST-MULTIPARTFORMDATA) with URL. HTTP METHOD indirector have 3 types.
    • GenericHTTPClient::GetMethod is HTTP GET REQUEST
    • GenericHTTPClient::PostMethod is HTTP POST REQUEST
    • GenericHTTPClient::PostMultiPartsFormData is HTTP POST REQUEST with BINARY FORM DATA
  • RequestOfURI(...) method is that you could have attempt request for HTTP REQUEST with URI. This method is used with Connect(...) and Close().
  • Response(...) method is that you have HTTP Response by BYTES.
  • QueryHTTPResponse() method is you have receive HTML of your HTTP REQUEST.
  • QueryHTTPResponseHeader() method is you have receive HTTP HEADER about QueryHTTPResponse().
  • GetLastError() method is you get windows error code.
  • GetContentType(...) method is you have get MIME-TYPE.
  • ParseURL(...) method parse URL to protocol(HTTP, HTTPS) and address(or hostname) and port, URI.

Usage

Now, you have to simply do  HTTP GET REQUEST iteration. <PRE lang=c++>.... GenericHTTPClient httpClient; if(httpRequest.Request("http://www.codeproject.com")){ LPCTSTR szHTML=httpRequest.QueryResponse(); }else{ LPVOID lpMsgBuffer; DWORD dwRet=FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, httpRequest.GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPTSTR>(&lpMsgBuffer), 0, NULL); MessageBox( reinterpret_cast<LPTSTR>(lpMsgBuffer), "ERROR", MB_OK); LocalFree(lpMsgBuffer); }

This is HTTP POST REQUEST with file posting ( HTTP POST Multipart/form-data) <PRE lang=c++>GenericHTTPClient *pClient=new GenericHTTPClient(); pClient->InitilizePostArguments(); pClient->AddPostArguments(__TAG_USRID, szUserID); pClient->AddPostArguments(__TAG_JUMIN, szSocialIndex); pClient->AddPostArguments(__TAG_SRC, szSource); pClient->AddPostArguments(__TAG_DST, szDestination); pClient->AddPostArguments(__TAG_FORMAT, szFormat); pClient->AddPostArguments(__TAG_SUBJECT, szMessage); if(bCharge){ pClient->AddPostArguments(__TAG_CHARGE, "Y"); }else{ pClient->AddPostArguments(__TAG_CHARGE, "N"); } pClient->AddPostArguments(__TAG_CPCODE, szCPCode); pClient->AddPostArguments(__TAG_FILE, szFile, TRUE); if(pClient->Request(szURL, GenericHTTPClient::RequestPostMethodMultiPartsFormData)){ LPCTSTR szResult=pClient->QueryHTTPResponse(); }else{ #ifdef _DEBUG LPVOID lpMsgBuffer; DWORD dwRet=FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, pClient->GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPTSTR>(&lpMsgBuffer), 0, NULL); OutputDebugString(reinterpret_cast<LPTSTR>(lpMsgBuffer)); LocalFree(lpMsgBuffer); #endif } }

Thanks

Thanks that you have been reading my article and my bad English. ;p

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Heo Yongseon
Software Developer
Korea (Republic of) Korea (Republic of)
Poke tongue | ;-P

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionStill one of the best free emailers out there - but can't get it working in Unicode Pin
Michael B Pliam29-Jan-13 19:36
memberMichael B Pliam29-Jan-13 19:36 
AnswerRe: Still one of the best free emailers out there - but can't get it working in Unicode Pin
Robert Fransson29-May-14 6:56
memberRobert Fransson29-May-14 6:56 
QuestionMany and many ERRORS.... Pin
Aric Green12-Sep-11 17:39
memberAric Green12-Sep-11 17:39 
GeneralMy vote of 5 Pin
william cc6-Oct-10 17:57
memberwilliam cc6-Oct-10 17:57 
GeneralUnable to compile using VC 2005 Pin
Michael B Pliam23-Jun-09 7:34
memberMichael B Pliam23-Jun-09 7:34 
I have gotten alot of use from this class in the past. But in trying to recompile on Visual Studio 2005, I've run into this problem:

typedef struct __GENERIC_HTTP_ARGUMENT{ // ARGUMENTS STRUCTURE
TCHAR szName[__SIZE_HTTP_ARGUMENT_NAME];
TCHAR szValue[__SIZE_HTTP_ARGUMENT_VALUE];
DWORD dwType;
operator==(const __GENERIC_HTTP_ARGUMENT &argV){
return !_tcscmp(szName, argV.szName) && !_tcscmp(szValue, argV.szValue);
}
} GenericHTTPArgument;


Error 1 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

This occurs at the line beginning with 'operator'. I do not know how to fix it. Can anyone help? Smile | :)
GeneralRe: Unable to compile using VC 2005 Pin
Michael B Pliam23-Jun-09 7:38
memberMichael B Pliam23-Jun-09 7:38 
GeneralError in Release mode Pin
Member 33281254-Jun-09 23:57
memberMember 33281254-Jun-09 23:57 
GeneralMy vote of 2 Pin
KarstenK26-Jan-09 22:01
memberKarstenK26-Jan-09 22:01 
QuestionHttpOpenRequest GetLastError return 122 Pin
caoxin.wh22-Dec-08 19:00
membercaoxin.wh22-Dec-08 19:00 
Generalcan't compile Pin
gu@z12-Nov-08 17:05
membergu@z12-Nov-08 17:05 
GeneralAdding headers Pin
Nirav Doshi29-Jun-08 10:06
memberNirav Doshi29-Jun-08 10:06 
AnswerRe: Adding headers [modified] Pin
Nirav Doshi2-Jul-08 2:49
memberNirav Doshi2-Jul-08 2:49 
GeneralRe: Adding headers Pin
Ray Gerami27-Jul-09 11:09
memberRay Gerami27-Jul-09 11:09 
GeneralReading binary data Pin
Edyb27-Jun-08 5:10
memberEdyb27-Jun-08 5:10 
GeneralUpload file with size greater that 3MB Pin
hishree24-Jun-08 22:06
memberhishree24-Jun-08 22:06 
QuestionSupport for HTTPS/SSL Pin
DavidJHubbard17-Jun-08 6:08
memberDavidJHubbard17-Jun-08 6:08 
Generalinvalid argument internetclosehandle argument1 hinternet:0x00cc0004 Pin
micosun29-Apr-08 9:11
membermicosun29-Apr-08 9:11 
QuestionWhy does the CheckConnectionExists() function fail when there is clearly an Internet Connection? Pin
Michael B Pliam24-Apr-08 18:31
memberMichael B Pliam24-Apr-08 18:31 
QuestionHelp about Upload the File(How to set the parameter) Pin
hishree16-Mar-08 21:36
memberhishree16-Mar-08 21:36 
GeneralSimple HTTP Client using WININET Pin
Asia_chenchen21-Sep-07 6:51
memberAsia_chenchen21-Sep-07 6:51 
Questionwhat is the usage 0f Response? Pin
sunwayland9-Jul-07 18:12
membersunwayland9-Jul-07 18:12 
Questionhelp me,how to send this post? Pin
shellhy4-Jul-07 1:40
membershellhy4-Jul-07 1:40 
GeneralBytes sent / received Pin
__TSX__23-Apr-07 3:02
member__TSX__23-Apr-07 3:02 
Generalsuck Pin
_ppr10-Dec-06 1:55
member_ppr10-Dec-06 1:55 
GeneralRe: suck Pin
H.J.Park26-Jul-11 17:55
memberH.J.Park26-Jul-11 17:55 
QuestionUnhandled exception at 0x7c84b273 in myProject.exe: 0xC0000005: Access violation reading location 0xcccccccc. Pin
juhtzyy31-Oct-06 21:02
memberjuhtzyy31-Oct-06 21:02 
AnswerRe: Unhandled exception at 0x7c84b273 in myProject.exe: 0xC0000005: Access violation reading location 0xcccccccc. Pin
Michael B Pliam1-Dec-06 17:16
memberMichael B Pliam1-Dec-06 17:16 
GeneralRe: Unhandled exception at 0x7c84b273 in myProject.exe: 0xC0000005: Access violation reading location 0xcccccccc. Pin
Earnol23-Apr-07 1:03
memberEarnol23-Apr-07 1:03 
GeneralRe: Unhandled exception at 0x7c84b273 in myProject.exe: 0xC0000005: Access violation reading location 0xcccccccc. Pin
khanathar78626-Nov-07 5:37
memberkhanathar78626-Nov-07 5:37 
QuestionA HTTPSendRequest Problem Pin
jiangrenkuan26-Oct-06 22:31
memberjiangrenkuan26-Oct-06 22:31 
GeneralRe: A HTTPSendRequest Problem Pin
raghup1327-Oct-06 0:34
memberraghup1327-Oct-06 0:34 
AnswerRe: A HTTPSendRequest Problem Pin
shellhy22-Jul-07 4:10
membershellhy22-Jul-07 4:10 
GeneralError LNK2005 Pin
Alex_j2-Sep-06 3:37
memberAlex_j2-Sep-06 3:37 
Generalthe SESSIONID can't change Pin
shellhy14-Aug-06 8:54
membershellhy14-Aug-06 8:54 
QuestionHow to to POST Data? Pin
Adeel6887-Aug-06 3:35
memberAdeel6887-Aug-06 3:35 
GeneralCan't compile Pin
Adeel6887-Aug-06 3:24
memberAdeel6887-Aug-06 3:24 
GeneralRe: Can't compile Pin
andriuha13-Sep-06 3:14
memberandriuha13-Sep-06 3:14 
GeneralNeed help in uploading image Pin
ranu_hai28-Jul-06 1:02
memberranu_hai28-Jul-06 1:02 
Generalplease give me a next version Pin
shellhy1-Jun-06 2:42
membershellhy1-Jun-06 2:42 
QuestionHow do you format a POST string to produce a line break in the HTML output? [modified] Pin
Michael B Pliam26-May-06 8:13
memberMichael B Pliam26-May-06 8:13 
GeneralProblem with AddPostArguments Pin
Michael B Pliam22-May-06 5:48
memberMichael B Pliam22-May-06 5:48 
GeneralRe: Problem with AddPostArguments Pin
Mike Pliam22-May-06 14:13
memberMike Pliam22-May-06 14:13 
GeneralRe: solution Pin
arieharieh26-May-06 2:51
memberarieharieh26-May-06 2:51 
GeneralRe: solution Pin
Michael B Pliam26-May-06 7:46
memberMichael B Pliam26-May-06 7:46 
GeneralIt's nice module But found one Error maybe.. Pin
nami034218-May-06 3:46
membernami034218-May-06 3:46 
GeneralError C2065 : "httpRequest" : no declaration identifier Pin
langzi011526-Apr-06 19:56
memberlangzi011526-Apr-06 19:56 
GeneralRe: Error C2065 : "httpRequest" : no declaration identifier Pin
Member 853086830-Jan-12 4:39
memberMember 853086830-Jan-12 4:39 
Generalcan't compile Pin
nubz22-Mar-06 1:37
membernubz22-Mar-06 1:37 
GeneralRe: can't compile Pin
Just do it4-Apr-06 6:33
memberJust do it4-Apr-06 6:33 
QuestionHelp - Login to internet sites Pin
ddalmia20008-Dec-05 18:28
memberddalmia20008-Dec-05 18:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 3 Sep 2003
Article Copyright 2003 by Heo Yongseon
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid