httprequest - HTTP Requests in C++ without external libraries? -
so question has been asked before, general answer pointed using external library such curlpp. curious if http requests done using standard libraries of c++14. how difficult be?
say example, wanted xml document , store in string parsed. steps have taken achieve this?
if curious, i'm doing learning experience better understand how http requests work.
it sounds me want implement http protocol scratch on top of posix sockets api. have done myself, quite fun.
read sockets api here: http://en.wikipedia.org/wiki/berkeley_sockets
if want work on windows, see here.
this link, posted in comments, provides pretty starting-point example using api, although weirdly includes both client , server serial logic within same program -- may allow bypass of calls (such waiting incoming connections) required implement client or server standalone program.
assuming implementing http server in c++, might choose implement client web page (running on favorite browser), following hack demonstrates...
<html> <head> </head> <body> web page sends entered text server upon button press. server's response displayed in box below. hack learning purposes, , not demonstration of best-practices. <br> <textarea id="inp"> hello world! </textarea> <br> <button onclick="return make_request('inp','get','test1')">get request</button> <button onclick="return make_request('inp','post','test2')">post request</button> <div id="result" style='border-style:solid;'>?</div> <script> function make_request( textid, request_type, webfn ) { var url = "" + "?webfn="+webfn // assumes page served same server if ( request_type != 'post' ) url += "&value="+document.getelementbyid(textid).value; var req = new xmlhttprequest(); req.open( request_type, url, /*async*/false ); req.send( request_type=='post' ? document.getelementbyid(textid).value : null ); if ( req.readystate == 4/*complete*/ && req.status == 200/*ok*/ ) { result = req.responsexml.documentelement.getelementsbytagname('value')[0].firstchild.data; document.getelementbyid('result').innerhtml = req.responsetext; } else alert("http request failed"); return false; } </script> </body> </html>
Comments
Post a Comment