Eamorr
Eamorr

Reputation: 10012

C: how to read a webpage

I'm trying to open a connection to a webpage (e.g. www.google.com) via localhost, port 80.

How can I do this programatically in C? I want get all the HTML headers and not just the content ;(

I hope someone can help.

Many thanks in advance,

Upvotes: 4

Views: 7322

Answers (3)

Minimal runnable POSIX example

In this answer, I provide a minimal runnable POSIX C example: How to make an HTTP get request in C without libcurl?

It allows you to do:

./wget example.com

to download http://example.com

Upvotes: 0

eyalm
eyalm

Reputation: 3366

Summarized process:

  • DNS resolution for the hostname (using getaddrinfo())
  • Open a stream socket (TCP) to the resolved IP address and port
  • Send GET request (see protocol in: http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol)

    GET /index.html HTTP/1.1 Host: www.example.com

  • Read headers - Terminated by \r\n\r\n

  • Read body
  • Close socket

Upvotes: 2

skorks
skorks

Reputation: 4366

Here is some example code on how to do this with libcurl:

http://curl.haxx.se/libcurl/c/getinmemory.html

There is another one right there, that shows you how to get some header data:

http://curl.haxx.se/libcurl/c/getinfo.html

These examples and many others are available as part of the libcurl distribution. It should more than get you started.

Upvotes: 4

Related Questions