1

So this question has been asked before, but the general answer pointed to was using an external library such as cURLpp. So I was curious as to if HTTP requests could be done using only the standard libraries as of C++14. How difficult would this be?

Say for example, I wanted to get an XML document and store it in a string to be parsed. What steps would have to be taken to achieve this?

If anyone is curious, I'm doing this as a learning experience to better understand how HTTP requests work.

Brandon
  • 259
  • 1
  • 3
  • 12
  • There is nothing in the standard libraries, so it is not possible. – Jesse Good May 14 '15 at 03:24
  • 3
    There is no notion of socket in standard library, you're out of luck – Severin Pappadeux May 14 '15 at 03:27
  • To be more precise, on system like Plan 9 you could establish network connection just doing pseudofile I/O, so I would venture it would be possible – Severin Pappadeux May 14 '15 at 03:28
  • 1
    The best option probably is to use Boost.Asio as it will be [standardized](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4370.html) sometime in the future. – Praetorian May 14 '15 at 03:29
  • So what about making my own library for this? @Praetorian So this is a planned feature for the standard library? – Brandon May 14 '15 at 03:30
  • 2
    Yes, the document I linked to is the proposal for standardization – Praetorian May 14 '15 at 03:34
  • 2
    You have to use a socket library for the communication. However you can do HTTP without using `CURL` its a simple text based protocol. But, for less than trivial tasks, you end up reinventing so many wheels that you might as well have used `CURL` to begin with. But for basic tasks I use sockets all the time (after wrapping them in a `std::iostream`). Its very light-weight and solves a dependency issue. – Galik May 14 '15 at 03:52
  • Does this help? http://stackoverflow.com/questions/22077802/simple-c-example-of-doing-an-http-post-and-consuming-the-response/22135885#22135885 – Jerry Jeremiah May 14 '15 at 04:44

1 Answers1

2

It sounds like to me that you want to implement the HTTP protocol from scratch on top of the POSIX sockets API. I have done this myself, it was quite fun.

Read about the sockets API here: http://en.wikipedia.org/wiki/Berkeley_sockets

If you want to work on Windows, see here.

This link, posted in the comments, provides a pretty good starting-point example for using the API, although it weirdly includes both the client and the server as serial logic within the same program -- which may allow it to bypass some of the calls (such as waiting for incoming connections) required to implement a client or server as a standalone program.

Assuming you are implementing an HTTP server in C++, you might choose to implement the client as a web page (running on your favorite browser), as the following hack demonstrates...

<html>
<head>
</head>
<body>
This web page sends the entered text back to the server upon a button press.
The server's response is then displayed in the box below.
This is only a hack for learning purposes, and not a 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 this page was served by the 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>
Community
  • 1
  • 1
Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
  • Yes I believe this is what I want to do. I'm on Linux, but I would like to make this cross-platform. – Brandon May 14 '15 at 14:13