views:

6369

answers:

11

Is there any way to easily make a HTTP request with C++, are there any libraries that do this. Specifically, I want to download the contents of a page (an API) and check the contents to see if it contains a 1 or a 0. Is it also possible to download the contents into a string?

Code samples would be good.

+13  A: 

libcURL is a fairly lightweight and easy to use API that supports HTTP.

Gerald
There is a C++ binding to libCURL at http://curlpp.org/
Michael E
+1  A: 

These both are C (not C++) libs but they might be useful: libcURL which is the most popular or Libwww which is not so popular but is sponsored by W3C.

victor hugo
+2  A: 

If you are on windows you can used WinInet or winHTTP APIs. There are lots of code samples available on internet using wininet.

Alien01
+3  A: 

C++ does not provide any way to do it directly. It would entirely depend on what platforms and libraries that you have.

At worst case, you can use the boost::asio library to establish a TCP connection, send the HTTP headers (RFC 2616), and parse the responses directly. Looking at your application needs, this is simple enough to do.

sybreon
A: 

You could write your own. It's not too difficult...

graham.reeds
Actually it is not that easy. HTTP is not a trivial protocol. Issuing a simple GET request is easy, but when things such as redirects or working behind proxies come into play, things become complicated.
kgiannakakis
+1  A: 

You should also check out Urdl

+1  A: 

Some other possibilities:

Éric Malenfant
+5  A: 

libCURL is a pretty good option for you. Depending on what you need to do, the tutorial should tell you what you want, specifically for the easy handle. But, basically, you could do this just to see the source of a page:

CURL* c;
c = curl_easy_init();
curl_easy_setopt( c, CURL_URL, "www.google.com" );
curl_easy_perform( c );
curl_easy_cleanup( c );

I believe this will cause the result to be printed to stdout. If you want to handle it instead -- which, I assume, you do -- you need to set the CURL_WRITEFUNCTION. All of that is covered in the curl tutorial linked above.

FreeMemory
+1  A: 

C and C++ don't have a standard library for HTTP or even for socket connections. Over the years some portable libraries have been developed. The most widely used, as others have said, is libcurl.

Here is a list of alternatives to libcurl (coming from the libcurl's web site).

Also, for Linux, this is a simple HTTP client. You could implement your own simple HTTP GET client, but this won't work if there are authentication or redirects involved or if you need to work behind a proxy. For these cases you need a full-blown library like libcurl.

For source code with libcurl, this is the closest to what you want (Libcurl has many examples). Look at the main function. The html content will be copied to the buffer, after a successfully connection. Just replace parseHtml with your own function.

kgiannakakis
+9  A: 

I had the same problem. libcurl is really complete. There is a C++ wrapper curlpp that might interest you as you ask for a C++ library. neon is another interesting C library that also support webdav.

curlpp seems natural if you use C++. There are many examples provided. To get the content of an URL you do something like that (extracted from examples) :

// RAII cleanup
curlpp::Cleanup myCleanup;

// standard request object.
curlpp::Easy myRequest;

// Set the URL.
myRequest.setOpt<Url>("http://example.com");

// Send request and get a result.
// By default the result goes to standard output.
// Here I use a shortcut to get it in a string stream ...
std::ostringstream os;
os << myRequest.perform();

string asAskedInQuestion = os.str();

my 2 cents ...

neuro
+3  A: 

As you want a C++ solution, you could use Qt. It has a QHttp class you can use.

You can check the docs:

http->setHost("qtsoftware.com");
http->get(QUrl::toPercentEncoding("/index.html"));

Qt also has a lot more to it that you could use in a common C++ app.

Marcelo Santos