tags:

views:

247

answers:

5

I hate CURL it is too bulky with too many dependencies when all I need to do is quickly open a URL. I don't even need to retrieve the contents of the web page, I just need to make the GET HTTP request to the server.

What's the most minimal way I can do this and don't say CURL !@#$

+4  A: 

There are lots of choices! Try libwww -- but be warned, most people strongly prefer libcurl. It's much easier to use.

John Feminella
+1  A: 

system("wget -q -O file.htm http://url.com");

Carl Smotricz
Of course this requires wget exist on the target machine.
Billy ONeal
+2  A: 

There's a very light way and I've done this myself when implementing a high-scale back end service for a large media provider in the UK.

This method is extremely operating-system specific.

  1. open a TCP socket to the HTTP server
  2. send a "GET /path/to/url HTTP/1.1\r\nHost: www.host.com\r\n\r\n" (the Host header is required for HTTP/1.1 and most virtual servers, don't forget the two blank lines, and a newline requires a carriage return as well for HTTP headers)
  3. wait for the response
  4. close the socket

If you are going to close the socket at the end of the connection you may also want to send a Connection: close\r\n as part of your headers to inform the web server that you will terminate the connection after retrieving the web page.

You may run into trouble if you're fetching an encoded or generated web page in which case you'll have to add logic to interpret the fetched data.

PP
If you are using Win32 then before you call any socket functions you must initialise the Windows sockets layer. See http://www.nullterminator.net/winsock.html for a tutorial - note that you want to create a client, not a server.
PP
+3  A: 

On Windows (Windows XP, Windows 2000 Professional with SP3 and above) you could use WinHttpReadData API. There's also an example at the bottom of that page.

More info on Windows HTTP Services on MSDN

Indeera
A: 

I have used Winsock when I need as few dependencies as possible and it has worked well. You need to write more code than using a separate library or the Microsoft WinHTTP library.

The functions you need are WSAStartup, socket, connect, send, recv, closesocket and WSACleanup.
See sample code for the send function.

Peter Olsson