tags:

views:

124

answers:

3

In PHP I can do it as simple as :

file_get_contents('http://stackoverflow.com/questions/ask');

What's the shortest code to do the same in C?

UPDATE

When I compile the sample with curl, got errors like this:

unresolved external symbol __imp__curl_easy_cleanup referenced in function _main 
+2  A: 

Try the libcurl C interface

David Sykes
+10  A: 

Use libcurl, refer to their example C snippets

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
    res = curl_easy_perform(curl);

    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  return 0;
}
Richard Harrison
+0.3 for the clear and concise answer, +0.7 for using the swedish top-level domain =)
manneorama
@manneorama :-) - Thanks although I should point out that we shouldn't use curl.haxx.se for testing instead maybe use www.sweden.gov.se
Richard Harrison
But I got errors like this when compiling it: `unresolved external symbol __imp__curl_easy_cleanup referenced in function _main`
ieplugin
I dont find any lib files in its source distribution either..
ieplugin
I don't believe there are libs in the source distribution. You should build it yourself, shouldn't you?
Vitor Py
+1  A: 

I should have commented the Richard Harrison good answer, but I have not 50 reputations points yet, so I put here as an answer my hint to ieplugin for compiling the code:

On Ubuntu 10.04 (and supposing you named the source file getpage.c):

sudo apt-get install libcurl4-dev
gcc getpage.c -lcurl -o getpage
Vanni Totaro
how to compile it from source?
ieplugin
@`ieplugin`: I think it would be better if you ask a new question on `curl` compiling and accept `Richard` answer to this question.
Vanni Totaro