By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,441 Members | 998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,441 IT Pros & Developers. It's quick & easy.

How to get a HTTP Status code in a C program?

P: 8
Hey guys, I'm trying to make a program that checks a specific internet address and gives me back the HTTP Status Code, I have to do it in C because it's a school project.

I'm using the Cygwin compiler on a Windows 7 (64-bit version)

I've searched Google for some good examples, but all I come across is C# and C++ codes.

Expand|Select|Wrap|Line Numbers
  1. HttpWebRequest HttpWReq =
  2. (HttpWebRequest)WebRequest.Create("http://www.google.com/services/");
  3. HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
  4. Response.Write(HttpWResp.StatusCode);
  5.  
This is the code I've found that makes the much sense to me but as far as I know, this has nothing to do with C.

Any suggestions / code examples I can cast my eyes on?

As for suggestions, what the full purpose of this is, is to check if a specific webpage is a valid internet link.

Two examples:
www.google.com/services/ is a valid link (This page exist)
www.google.com/something/ is not a valid link (This page do not exist)

I've also tried it with this code:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <netdb.h>
  6. #include <arpa/inet.h>
  7.  
  8. void tjeklinkonline(char *linkIND) {
  9.  
  10.     struct addrinfo hints, *res, *p;
  11.     int status;
  12.     char ipstr[INET6_ADDRSTRLEN];
  13.  
  14.     // Determines IP-type and Socket
  15.  
  16.     memset(&hints, 0, sizeof hints);
  17.     hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
  18.     hints.ai_socktype = SOCK_STREAM;
  19.  
  20.     //Pings net address, and write response
  21.  
  22.     if ((status = getaddrinfo(linkIND , NULL, &hints, &res)) != 0) {
  23.         fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
  24.         printf("Not a Valid Link \n");
  25.     }
  26.     else {
  27.     printf("Valid Link\n");
  28.     }
  29.  
  30.     freeaddrinfo(res); // free the linked list
  31. } // End of function
  32.  
  33.  
  34.  
  35.  
  36.  
  37. int main(void)
  38. {
  39.  
  40.   char link[40] = "www.google.com";
  41.   char link2[40] = "www.google.com/services/";
  42.  
  43.   tjeklinkonline(link2);
  44.  
  45.  
  46.     return 0;
  47. }
  48.  
But this code, is only capable of contacting domain addresses which just isn't enough.

Thanks in advance
Nov 18 '10 #1

✓ answered by Lars Pedersen

Okay... So now I've fixed my problem, thanks for the guidance though.

So to do this short, if you want to understand how the network contacts work, try to understand how Sockets work.

This place explains it pretty well:
http://www.linuxhowtos.org/C_C++/socket.htm

And then go and get cURL, if you use Cygwin, you can just include the cURL package when you install it.

And in order to learn cURL, well... Go to their page :)
http://curl.haxx.se/

And then the function you will need after understanding how cURL works would be, curl_easy_getinfo()

More on that here:
http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html

Hope this is enough for anyone having the same problem to get it done :)
If you need an actual example, I would be happy to help.

Share this Question
Share on Google+
6 Replies


100+
P: 1,059
To read HTTP Status code you need to connect with the HTTP server,
Send HEADER Request(not POST, not GET) and then parse from it.

Do you know how to connect with HTTP server.

??
Nov 18 '10 #2

P: 8
Not really no, I'm new to programing, and I've only mastered the general functions in C

But now I've found a page:
http://msdn.microsoft.com/en-us/libr...=VS.85%29.aspx

That seams to have all the functions I'll need, but I'm just a tad confused as of how the code should look when I establish the connection, could you give me an example?
Nov 18 '10 #3

100+
P: 1,059
First Learn SOCKET
Then study a little about HTTP. You will get thousands of application/examples which can teach you a bit about how to develop SOCKET based application. ......
Nov 19 '10 #4

P: 8
Hey again Johny, I've been studying the SOCKETS and I'd say I understood it enough to start making my first attempt on making my own program using SOCKETS, but I'm trying to compile this code that I found at http://www.linuxhowtos.org/C_C++/socket.htm but it won't compile. And I'm thinking I might have a problem with my Cygwin setup?

I get the Error code:

onlinelink.c: In function 'main':
onlinelink.c:47: warning: passing arg 2 of 'connect' from incompatible pointer type

I've tried to cast it, but that doesn't help either.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <netdb.h> 
  6.  
  7. void error(char *msg)
  8. {
  9.     perror(msg);
  10.     exit(0);
  11. }
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15.  
  16. // Variables
  17.     int sockfd, portno, n;
  18.     struct sockaddr_in serv_addr;
  19.     struct hostent *server;
  20.     char buffer[256];
  21.  
  22.  
  23.     if (argc < 3) {
  24.        fprintf(stderr,"usage %s hostname port\n", argv[0]);
  25.        exit(0);
  26.     }
  27.  
  28.  
  29.     portno = atoi(argv[2]);
  30.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  31.  
  32.     if (sockfd < 0) 
  33.         error("ERROR opening socket");
  34.     server = gethostbyname(argv[1]);
  35.     if (server == NULL) {
  36.         fprintf(stderr,"ERROR, no such host\n");
  37.         exit(0);
  38.     }
  39.  
  40.     bzero((char *) &serv_addr, sizeof(serv_addr));
  41.     serv_addr.sin_family = AF_INET;
  42.     bcopy((char *)server->h_addr, 
  43.          (char *)&serv_addr.sin_addr.s_addr,
  44.          server->h_length);
  45.     serv_addr.sin_port = htons(portno);
  46.  
  47.     if (connect(sockfd , &serv_addr , sizeof(serv_addr)) < 0) 
  48.         error("ERROR connecting");
  49.  
  50.  
  51. // This last part is just to keep it as I found it, not really a part of the function I'm looking for.
  52.  
  53.  
  54.     printf("Please enter the message: ");
  55.     bzero(buffer,256);
  56.     fgets(buffer,255,stdin);
  57.     n = write(sockfd,buffer,strlen(buffer));
  58.     if (n < 0) 
  59.          error("ERROR writing to socket");
  60.     bzero(buffer,256);
  61.     n = read(sockfd,buffer,255);
  62.     if (n < 0) 
  63.          error("ERROR reading from socket");
  64.     printf("%s\n",buffer);
  65.     return 0;
  66. }
  67.  
If you'd take a look at it, it would really help me a lot.
Nov 24 '10 #5

P: 8
Never mind my last message, learned that it works anyway.

Just didn't know that you could run it even if you get a warning :P

But learned so at the university today ^^

So... Next is the HTTP! Hello world, here I come! :D

---------------

But where do I get a HTTP guide for C? To me, it doesn't look like HTTP functions is a standard thing in C.

But, I've read about cURL, should I download this function set and try using that?
Nov 25 '10 #6

P: 8
Okay... So now I've fixed my problem, thanks for the guidance though.

So to do this short, if you want to understand how the network contacts work, try to understand how Sockets work.

This place explains it pretty well:
http://www.linuxhowtos.org/C_C++/socket.htm

And then go and get cURL, if you use Cygwin, you can just include the cURL package when you install it.

And in order to learn cURL, well... Go to their page :)
http://curl.haxx.se/

And then the function you will need after understanding how cURL works would be, curl_easy_getinfo()

More on that here:
http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html

Hope this is enough for anyone having the same problem to get it done :)
If you need an actual example, I would be happy to help.
Nov 28 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.