Click here to Skip to main content
15,881,812 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.2K   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

 
GeneralGetPageStatusCode need GetHeaders [modified] Pin
Kikoa17-Sep-07 4:25
Kikoa17-Sep-07 4:25 
GeneralRe: GetPageStatusCode need GetHeaders Pin
kamplus23-Feb-11 21:57
kamplus23-Feb-11 21:57 
GeneralPosting Problens Pin
Jot Singh13-Jun-07 5:27
Jot Singh13-Jun-07 5:27 
GeneralRe: Posting Problens Pin
Albertino11-Aug-07 10:54
Albertino11-Aug-07 10:54 
GeneralCookie Support Pin
mhecker29-Dec-06 6:36
mhecker29-Dec-06 6:36 
QuestionHow to check if the data is successfully posted or not [modified] Pin
Adeel6887-Aug-06 1:17
Adeel6887-Aug-06 1:17 
Questionhow to use proxy? Pin
shellhy7-Jun-06 22:13
shellhy7-Jun-06 22:13 
AnswerRe: how to use proxy? Pin
DavidA20055-Mar-09 2:47
DavidA20055-Mar-09 2:47 
QuestionHow to Post XML - Data? Pin
Generator3Punkte5-Apr-06 23:21
Generator3Punkte5-Apr-06 23:21 
AnswerRe: How to Post XML - Data? Pin
vijicode22-Nov-06 1:09
vijicode22-Nov-06 1:09 
Newsnot thread-safe Pin
indrekm15-Dec-05 1:13
indrekm15-Dec-05 1:13 
GeneralHandling SSL certificates errors Pin
Orkblutt18-Oct-05 21:36
Orkblutt18-Oct-05 21:36 
Questionmemory leak? Pin
dark dog9-Sep-05 11:16
dark dog9-Sep-05 11:16 
AnswerRe: memory leak? Pin
dietox24-Sep-07 10:19
dietox24-Sep-07 10:19 
GeneralProblem :) Pin
zaixx7-Jul-05 11:27
zaixx7-Jul-05 11:27 
GeneralRe: Problem :) Pin
dietox6-Oct-07 3:08
dietox6-Oct-07 3:08 
GeneralProblem including header Pin
int01h24-Jun-05 11:00
int01h24-Jun-05 11:00 
GeneralRe: Problem including header Pin
Anonymous24-Jun-05 11:45
Anonymous24-Jun-05 11:45 
GeneralTransfer Unicode data Pin
Sitaram Sukumar3-Feb-05 20:04
Sitaram Sukumar3-Feb-05 20:04 
Generalgood work Pin
motosir7-Aug-04 4:47
motosir7-Aug-04 4:47 
GeneralGreat! Pin
Todd.Harvey2-Mar-09 8:34
Todd.Harvey2-Mar-09 8:34 
Generalto save the web page without opening in a browser Pin
Member 6566481-Dec-03 19:44
Member 6566481-Dec-03 19:44 
GeneralVery Nice code...But Pin
Sumit Kapoor20-Nov-03 0:56
Sumit Kapoor20-Nov-03 0:56 
GeneralRe: Very Nice code...But Pin
y_yy200810-Dec-08 16:13
y_yy200810-Dec-08 16:13 
GeneralRe: Very Nice code...But Pin
Sumit Kapoor12-Dec-08 13:02
Sumit Kapoor12-Dec-08 13:02 

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.