views:

172

answers:

1

If it's any help, there is also a similar class in C#'s WebRequest. Although I do not want it in java or .NET, i am wondering how to implement this in native C/C++ code (for windows).

for reference:

try {

URL url=new URL("http://google.ca");
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.connect();
int code = con.getResponseCode();
System.out.println(code);

} catch (MalformedURLException e) {
System.err.println("Error reading URL.");
}

prints out:

200

meaning "OK"

I understand I probably need to use sockets and send a User-Agent string, but I haven't a clue where to begin. Whenever I learn a new language the first thing I like to do is try porting my code to it, but this one has stumped me.

Any help is appreciated

+2  A: 

There is no HTTP support in standard C library.

So you have two options - use 3rd-party HTTP library, such ar libcurl, or handle HTTP yourself:

  • open socket
  • resolve hostname
  • connect to server
  • build HTTP request
  • send request to server
  • receive HTTP response
  • parse response and get response code from it.
qrdl