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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lars Pedersen
    New Member
    • Nov 2010
    • 8

    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.

    Code:
    HttpWebRequest HttpWReq =
    (HttpWebRequest)WebRequest.Create("http://www.google.com/services/");
    HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
    Response.Write(HttpWResp.StatusCode);
    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:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <arpa/inet.h>
    
    void tjeklinkonline(char *linkIND) {
    
        struct addrinfo hints, *res, *p;
        int status;
        char ipstr[INET6_ADDRSTRLEN];
     
        // Determines IP-type and Socket
    
        memset(&hints, 0, sizeof hints);
        hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
        hints.ai_socktype = SOCK_STREAM;
    
        //Pings net address, and write response
    
        if ((status = getaddrinfo(linkIND , NULL, &hints, &res)) != 0) {
            fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
    		printf("Not a Valid Link \n");
        }
    	else {
    	printf("Valid Link\n");
    	}
    
        freeaddrinfo(res); // free the linked list
    } // End of function
    
    
    
    
    
    int main(void)
    {
    
      char link[40] = "www.google.com";
      char link2[40] = "www.google.com/services/";
    
      tjeklinkonline(link2);
    
    	
        return 0;
    }
    But this code, is only capable of contacting domain addresses which just isn't enough.

    Thanks in advance
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    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.

    ??

    Comment

    • Lars Pedersen
      New Member
      • Nov 2010
      • 8

      #3
      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:
      The following list provides concise descriptions of each Winsock function. For additional information on any function, click the function name.


      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?
      Last edited by Lars Pedersen; Nov 18 '10, 01:04 PM. Reason: More information gathered.

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        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. ......

        Comment

        • Lars Pedersen
          New Member
          • Nov 2010
          • 8

          #5
          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.

          Code:
          #include <stdio.h>
          #include <sys/types.h>
          #include <sys/socket.h>
          #include <netinet/in.h>
          #include <netdb.h> 
          
          void error(char *msg)
          {
              perror(msg);
              exit(0);
          }
          
          int main(int argc, char *argv[])
          {
          
          // Variables
              int sockfd, portno, n;
              struct sockaddr_in serv_addr;
              struct hostent *server;
              char buffer[256];
          
          
              if (argc < 3) {
                 fprintf(stderr,"usage %s hostname port\n", argv[0]);
                 exit(0);
              }
          
          
              portno = atoi(argv[2]);
              sockfd = socket(AF_INET, SOCK_STREAM, 0);
          
              if (sockfd < 0) 
                  error("ERROR opening socket");
              server = gethostbyname(argv[1]);
              if (server == NULL) {
                  fprintf(stderr,"ERROR, no such host\n");
                  exit(0);
              }
          
              bzero((char *) &serv_addr, sizeof(serv_addr));
              serv_addr.sin_family = AF_INET;
              bcopy((char *)server->h_addr, 
                   (char *)&serv_addr.sin_addr.s_addr,
                   server->h_length);
              serv_addr.sin_port = htons(portno);
          
              if (connect(sockfd , &serv_addr , sizeof(serv_addr)) < 0) 
                  error("ERROR connecting");
          
          
          // This last part is just to keep it as I found it, not really a part of the function I'm looking for.
          
          
              printf("Please enter the message: ");
              bzero(buffer,256);
              fgets(buffer,255,stdin);
              n = write(sockfd,buffer,strlen(buffer));
              if (n < 0) 
                   error("ERROR writing to socket");
              bzero(buffer,256);
              n = read(sockfd,buffer,255);
              if (n < 0) 
                   error("ERROR reading from socket");
              printf("%s\n",buffer);
              return 0;
          }
          If you'd take a look at it, it would really help me a lot.

          Comment

          • Lars Pedersen
            New Member
            • Nov 2010
            • 8

            #6
            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?
            Last edited by Lars Pedersen; Nov 25 '10, 04:39 PM. Reason: Needed more help...

            Comment

            • Lars Pedersen
              New Member
              • Nov 2010
              • 8

              #7
              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:


              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 :)


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

              More on that here:


              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.

              Comment

              Working...