Find the answer to your Linux question:
Results 1 to 4 of 4
i want to write a C programr which sends HTTP HEAD request and read its content into structure or some file.Can anybody help me ?...
  1. #1
    Just Joined!
    Join Date
    Sep 2007
    Posts
    2

    http head request

    i want to write a C programr which sends HTTP HEAD request and read its content into structure or some file.Can anybody help me ?

  2. #2
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Nobody is going to do your homework for you. If you can come up with some code and have a specific issue you are stuck on in the code, maybe then someone will help. I suggest you look up some tutorials on sockets programming in C if you are totally lost, or tell your instructor you are totally lost.

  3. #3
    Just Joined!
    Join Date
    Sep 2007
    Posts
    2

    will work for the head request

    here is the code and this works for GET request but don't work for HEAD request.
    #include <stdlib.h>
    #include <stdio.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <sys/socket.h>
    #include <unistd.h>
    #include <string.h>

    /* Print the contents of the home page for the server's socket.
    Return an indication of success. */

    void get_home_page (int socket_fd)
    {
    printf("there we go\n");
    char buffer[10000];
    ssize_t number_characters_read;

    /* Send the HTTP GET command for the home page. */
    sprintf (buffer, "HEAD http://www.google.lk/index.html\n");
    write (socket_fd, buffer, strlen (buffer));
    /* Read from the socket. read may not return all the data at one
    time, so keep trying until we run out. */
    while (1) {
    number_characters_read = read (socket_fd, buffer, 10000);
    if (number_characters_read == 0)
    return;
    /* Write the data to standard output. */
    fwrite (buffer, sizeof (char), number_characters_read, stdout);
    printf("there we go BECAUSE");
    }
    }

    int main (int argc, char* const argv[])
    {
    int socket_fd;
    struct sockaddr_in name;
    struct hostent *hostinfo;
    printf("there we go\n");
    /* Create the socket. */
    socket_fd = socket (AF_INET, SOCK_STREAM, 0);
    /* Store the server's name in the socket address. */
    name.sin_family = AF_INET;
    /* Convert from strings to numbers. */
    hostinfo = gethostbyname (argv[1]);
    if (hostinfo == NULL)
    return 1;
    else
    name.sin_addr.s_addr = ((struct in_addr *)(hostinfo->h_addr))->s_addr;
    /* Web servers use port 80. */
    name.sin_port = htons (312;
    printf("there we go\n");
    /* Connect to the web server */
    if (connect (socket_fd,(struct sockaddr *)&name,sizeof (name)) == -1) {
    perror ("connect");
    return 1;
    }
    /* Retrieve the server's home page. */
    printf("there we go\n");
    get_home_page (socket_fd);

    return 0;
    }

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    If the GET works by taking your program and saying GET instead of HEAD, you're quite fortunate, because either the GET or the HEAD request as it appears in your program is incomplete.

    For a one-line request like that to be complete, you need to add the HTTP version number at the end of the request line. And it had better be 1.0, not 1.1, because HTTP 1.1 requires more info in the request than just this one line.

    To find out how to do this in HTTP version 1.0 (don't help him with his homework, guys!), look at RFC 1945.

    For information about how RFC's work, complete with links to the RFC's, go here.

    Also, just to be kosher, end the request line with \r\n, not just \n.

    Hope this helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...