473,386 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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.

6 11391
johny10151981
1,059 1GB
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
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
johny10151981
1,059 1GB
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
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
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
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

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

Similar topics

2
by: Geoff Soper | last post by:
I would like to get the HTTP status code of a URL using PHP. I think CURL is the way to go but I can't see how to do it. I see I can use curl_getinfo on the CURL session after I've curl_exec'ed it...
3
by: Don Miller | last post by:
I used a Response.Redirect "page.asp" as the last command in ASP code that processes a submitted form from an Adobe PDF form (using HTML format). For some reason, if there is any linked file...
3
by: probashi | last post by:
Hi, I have an ASP page that inserts a record into database. Some times I find that multiple records are being inserted (with same data) in to database. I checked IIS log and found the following:...
8
by: Jon Maz | last post by:
Hi All, Here's the code: HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://www.asdfasdfasdfafsd.com"); HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();...
0
by: BuddyWork | last post by:
Hello, I'm doing a performance test and my test graudually increases the connections to the webservice. Everything works well until my connection hits around 350 connections. Then future...
6
by: Nick Horrocks | last post by:
I have set up a custom error page for 404 errors. However the HTTP status code returned is 302 followed by 200, this causes search engines not to remove old pages from their index. How can I...
1
by: George | last post by:
Hi, I have a production IIS server with ASPX application. In logs i sometimes see 206 HTTP code which means "Partial Content". It happens only on images though. Is it something i must be...
1
by: Kiky | last post by:
Hi guys, Does anyone know how to get a http status code? (ok=200, 404=not found, 500=internal server error)? I can catch exceptions from WebException when I check for a url, but I still can't...
4
by: buntyindia | last post by:
Hi, When we get request on server after processing that it writes back the response to client that also contains HTTP status code like 200 for Ok, 404 for page not found. How can I have access...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.