Http post requests are very easy to handle by using HTML forms and other versions where they are natively supported. basically, a request is nothing but a socket-based communication between the browser and web server. the Hypertext Transfer Protocol, HTTP/1.1 (http://www.w3.org/Protocols/rfc2616/rfc2616.html), documents the standards.
A client request looks like the following:
Post <br/> <post program> <br/> HTTP/1.0 <br/> Accept: */* <br/> User-Agent: mozilla/4.0 <br/> Content-Length: <length of parameters> <br/> Accept-language: En-US <br/> Accept-encoding: gzip, deflate <br/> HOST: <postname> <br/> Content-Type: Application/X-WWW-form-urlencoded <br/> authorization: Basic <base64 encoded Login Name: password> </P> <p> <param1 = val1 1_m2 = val2> <br/>
The server responds with the header part and body part of the message.
Header part:
HTTP/1.0 200 <br/> date: Thu 9 Dec 2004 12:23:50 GMT <br/> server: archive-appliance <br/> connection: Close <br/> Content-Type: text/XML <br/>
The HTTP/1.0 200 Status states everything went okay. for instance, it can be HTTP/1.0 404 if you made a wrong request. the body part is whatever the Post Script or program's output is.
/* <Br/> * http_post.cpp <br/> * by Uday chitragar-2004/DEC/01 <br/> * this software is provided 'as-is ', without any express or implied <br/> * warranty. in no event will the authors be held liable for any <br/> * damages arising from the use of this software. <br/> * permission is granted to anyone to use this software for any <br/> * purpose, including using cial applic Ations, and to alter it and <br/> * redistribute it freely, subject to the following restrictions: <br/> * 1. the origin of this software must not be misrepresented; you must <br/> * not claim that you wrote the original software. if you use this <br/> * software in a product, an acknowledgment in the product documentation <br/> * wocould be appreciated but is not required. <br/> * 2. Altered source versions must be plainly marked as such, and <br/> * must not be misrepresented as being the original software. <br/> * 3. this notice may not be removed or altered from any source <br/> * distribution. <br/> * <br/> **/</P> <p>/* <br/> * Notes: <br/> * this source demonstrates sending http post request to webserver from C ++ <br/> * This uses sockets hence can be compiled On Linux, UNIX, win <br/> */</P> <p> # define linux_ OS <br/> // # define win_ OS <br/> # DEFINE _ debug_print (X) /* x */</P> <p> // For commn <br/> # include <iostream> <br/> # include <string> <br/> # include <stdlib. h> <br/> # include <assert. h> </P> <p> # ifdef linux_ OS <br/> # include <netdb. h> <br/> # endif </P> <p> # ifdef win_ OS <br/> # include <winsock2.h> <br/> # endif </P> <p> # define send_rq (MSG) /<br/>/* cout <send_str ;* // <Br/> send (sock, MSG, strlen (MSG), 0); </P> <p> using namespace STD; <br/> // <EXE> hostname API parameters <br/> int request (char * hostname, char * API, char * parameters, string & message) <br/>{</P> <p> # ifdef win_ OS <br/>{< br/> wsadatawsadata; <br/> wsastartup (0x0101, & wsadata ); <br/>}< br/> # endif </P> <p> sockaddr_in sin; <br/> int sock = socket (af_inet, sock_stream, 0 ); <br/> If (sock =-1) {<br/> re Turn-100; <br/>}< br/> sin. sin_family = af_inet; <br/> sin. sin_port = htons (unsigned short) 80); </P> <p> struct hostent * host_addr = gethostbyname (hostname); <br/> If (host_addr = NULL) {<br/> _ debug_print (cout <"unable to locate host" <Endl); <br/> return-103; <br/>}< br/> sin. sin_addr.s_addr = * (int *) * host_addr-> h_addr_list); <br/> _ debug_print (cout <"port:" <sin. sin_port <", address: "<Sin. sin_addr.s_addr <Endl); </P> <p> If (connect (sock, (const struct sockaddr *) & sin, sizeof (sockaddr_in) =-1) {<br/> _ debug_print (cout <"Connect failed" <Endl); <br/> return-101; <br/>}</P> <p> string send_str; </P> <p> send_rq ("Post"); <br/> send_rq (API ); <br/> send_rq ("HTTP/1.0/R/N"); <br/> send_rq ("accept: */R/N "); <br/> send_rq ("User-Agent: Mozilla/4.0/R/N"); </P> <p> char content _ Header [1, 100]; <br/> sprintf (content_header, "Content-Length: % d/R/N", strlen (parameters )); <br/> send_rq (content_header); <br/> send_rq ("Accept-language: En-US/R/N "); <br/> send_rq ("Accept-encoding: gzip, deflate/R/N"); <br/> send_rq ("Host :"); <br/> send_rq ("hostname"); <br/> send_rq ("/R/N"); <br/> send_rq ("Content-Type: application/X-WWW-form-urlencoded/R/N "); </P> <p> // if you need to send a basic Authorization <br/> // string auth = "username: Password"; <br/> // figureout a way to encode test into base64! <Br/> // string authinfo = base64_encode (reinterpret_cast <const unsigned char *> (Auth. c_str (), Auth. length (); <br/> // string spassreq = "Authorization: Basic" + authinfo; <br/> // send_rq (spassreq. c_str (); </P> <p> send_rq ("/R/N"); <br/> send_rq ("/R/N "); <br/> send_rq (parameters); <br/> send_rq ("/R/N "); </P> <p> _ debug_print (cout <"#### header ###" <Endl); <br/> char c1 [1]; <br/> int L, line_length; <br /> Bool loop = true; <br/> bool bheader = false; </P> <p> while (loop) {<br/> L = Recv (sock, C1, 1, 0); <br/> If (L <0) loop = false; <br/> If (c1 [0] = '/N ') {<br/> If (line_length = 0) loop = false; </P> <p> line_length = 0; <br/> If (message. find ("200 ")! = String: NPOs) <br/> bheader = true; </P> <p >}< br/> else if (c1 [0]! = '/R') line_length ++; <br/> _ debug_print (cout <c1 [0]); <br/> message + = c1 [0]; <br/>}</P> <p> message = ""; <br/> If (bheader) {</P> <p> _ debug_print (cout <"#### body ###" <Endl); <br/> char P [1024]; <br/> while (L = Recv (sock, P, 1023,0)> 0) {<br/> _ debug_print (cout. write (P, L); <br/> P [l] = '/0'; <br/> message + = P; <br/>}</P> <p> _ debug_print (cout <message. c_str (); <br/>}else {<br/> retu Rn-102; <br/>}</P> <p> # ifdef win_ OS <br/> wsacleanup (); <br/> # endif </P> <p> return 0; <br/>}</P> <p> int main () {<br/> string message; <br/> int request ("www.somesite.com", "/post_url.pl", "Search = Hello & date = todat", string & message ); <br/> // message contains response! </P> <p >}</P> <p>
From http://www.codeguru.com/cpp/i-n/internet/http/article.php/c8813/
(Continued)
The code in this article is based on these standards and has been tested to act as an HTTP client to make a POST request and retrieve the response from web server. through the open socket, write and read are simple tasks to perform. sending the requests in order and in the correct format is essential.