Java implementation of getResponseCode () in C?

If it helps, there is a similar class in C # WebRequest. While I don't want this in java or .NET, I am wondering how to implement this in native C / C ++ (for Windows) code.

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.");
}

      

gives out:

200

      

means "ok"

I understand that I probably need to use sockets and send a User-Agent string, but I don't know where to start. Whenever I am learning a new language the first thing I like to do is try to port my code, but this one has surpassed me.

Any help is appreciated

0


source to share


1 answer


There is no HTTP support in the standard C library.

So you have two options, use a third party HTTP library like <libcurl, or handle the HTTP yourself:



  • open socket
  • resolve hostname
  • connect to server
  • build an HTTP request
  • send a request to the server
  • get HTTP response
  • parse the response and get the response code from it.
+2


source







All Articles