fetching a web page in C

Hello,

I'm a total newbie to HTTP commands, so I'm not sure how to do this. What I'd like is to write a C program to fetch the contents of a html page of a given address.

Could someone help with this?

Thanks in advance!

what I could immediately think of is

use ( wget url ) in a system with contents redirected to a file.

But this is the not right approach.

Else

frame a http request and post it to the port 80 of the needed url

You basically do

fd=socket(AF_INET,SOCK_STREAM,0);
connect(fd,remoteWebserver....)
send(fd,"GET / HTTP/1.0\r\n\r\n",18);
shutdown(fd,SD_SEND);
while ((i=recv(fd,buf,sizeof(buf),0))>0)
{
      write(1,buf,i);
}
close(fd);

However, as life is short, I recommend spawning wget so it can deal with http/ftp/https etc

You can deal with https by using OpenSSL to provide the TCP connectivity.

Don't reinvent the wheel! Use libcurl:

libcurl - C API