Click here to Skip to main content
15,890,845 members
Articles / Desktop Programming / MFC
Article

AmHttpUtilities

Rate me:
Please Sign up or sign in to vote.
4.61/5 (26 votes)
3 Sep 20011 min read 261.6K   3.1K   67   64
A small class that handles HTTP POST, GET and HEAD operations.

Introduction

I made this class because I needed to post some data to an ASP page. There are functions for getting a webpage, posting to a webpage, and getting the headers and the server status code. The class also works with Unicode. I have written the class using WinInet, so you have to link with wininet.lib.

Just make a new instance of the class, like:

CAmHttpSocket http;

The function GetPage() receives a webpage, or posts data to it.

char* GetPage(const TCHAR *url, bool Post = false, 
    const char *PostData = NULL, int PostDataLength = -1);

To simply receive a page just write:

char *s = http.GetPage(_T("http://somewebpage/");

The Post indicates whether you want to perform a HTTP POST or GET. PostData is the data you want to post to the webpage. This parameter is a char* because you cannot post Unicode strings. If PostData is NULL terminated, just leave PostDataLength as it is, else PostDataLength is the length of the PostData buffer.

If you want to post some data, that is a NULL terminated string, just write:

char *s = http.GetPage(_T("http://somewebpage/", true, "Some Data To Post");

If you just want to get the headers for a webpage you van use TCHAR* GetHeaders() to receive those.

TCHAR *ts = http.GetHeaders(_T("http://somewebpage/");

If you want to get the HTTP status code after you have received a webpage, or posted to one, just call GetPageStatusCode() like:

int i = http.GetPageStatusCode();

Now i contains the status code, like "404" if the page doesn't exists.

Well, that's all there is to it, hope you find it useful.

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


Written By
Software Developer (Senior)
Denmark Denmark
Huh! Wink | ;-)

Comments and Discussions

 
GeneralMod Pin
Baldcat1-Oct-03 18:49
Baldcat1-Oct-03 18:49 
Generalabout deconstructor function Pin
bsongo25-Aug-03 16:09
sussbsongo25-Aug-03 16:09 
QuestionDoes it work if a client connect to Internet via a proxy server? Pin
zxmhxj28-Jul-03 21:01
zxmhxj28-Jul-03 21:01 
AnswerRe: Does it work if a client connect to Internet via a proxy server? Pin
Hung-bin Chen6-Oct-03 18:10
Hung-bin Chen6-Oct-03 18:10 
GeneralRe: Does it work if a client connect to Internet via a proxy server? Pin
Anonymous3-May-04 23:53
Anonymous3-May-04 23:53 
GeneralBinary files Pin
inframan22-Jun-03 2:26
inframan22-Jun-03 2:26 
GeneralOpen post reply in browser Pin
Nicao29-May-03 6:28
Nicao29-May-03 6:28 
GeneralRe: Open post reply in browser Pin
Anders Molin29-May-03 9:03
professionalAnders Molin29-May-03 9:03 
QuestionHow to post data ? Pin
MrBean23-Mar-03 21:21
MrBean23-Mar-03 21:21 
AnswerRe: How to post data ? Pin
Anders Molin24-Mar-03 1:08
professionalAnders Molin24-Mar-03 1:08 
GeneralRe: How to post data ? Pin
MrBean24-Mar-03 4:36
MrBean24-Mar-03 4:36 
GeneralRe: How to post data ? Pin
Santiago Corredoira L.18-Apr-03 9:36
Santiago Corredoira L.18-Apr-03 9:36 
GeneralRe: How to post data ? Pin
Anders Molin5-May-03 4:28
professionalAnders Molin5-May-03 4:28 
AnswerRe: How to post data ? Pin
magic world6-Jul-03 3:11
magic world6-Jul-03 3:11 
GeneralRe: How to post data ? Pin
astrosmurfy21-Apr-05 3:02
astrosmurfy21-Apr-05 3:02 
AnswerRe: How to post data ? Pin
mohsen nourian19-Sep-04 4:15
mohsen nourian19-Sep-04 4:15 
QuestionAutomating a search Form ? Pin
ScorpioMidget13-Aug-02 4:58
ScorpioMidget13-Aug-02 4:58 
QuestionWhat about URLEncoding? Pin
Yariv Habot31-May-02 6:19
Yariv Habot31-May-02 6:19 
AnswerRe: What about URLEncoding? Pin
Anders Molin31-May-02 9:57
professionalAnders Molin31-May-02 9:57 
QuestionCache? Pin
6-May-02 22:58
suss6-May-02 22:58 
AnswerRe: Cache? Pin
Anders Molin31-May-02 9:51
professionalAnders Molin31-May-02 9:51 
GeneralRe: Cache? Pin
Vatara9-Feb-04 7:28
Vatara9-Feb-04 7:28 
QuestionWhat about SSL/PCS? Pin
Amit Dey12-Apr-02 2:08
Amit Dey12-Apr-02 2:08 
AnswerRe: What about SSL/PCS? Pin
Anders Molin8-May-02 11:30
professionalAnders Molin8-May-02 11:30 
GeneralRe: What about SSL/PCS? Pin
Amit Dey14-May-02 5:46
Amit Dey14-May-02 5:46 

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.