Sending http request to httpd

This is a discussion on Sending http request to httpd within the C Programming forums, part of the General Programming Boards category; Hi, I made this little program, the purpose was for it to send a GET /index.html HTTP/1.0 to a httpd. ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    24

    Sending http request to httpd

    Hi,

    I made this little program, the purpose was for it to send a GET /index.html HTTP/1.0 to a httpd.

    The only problem is, however, that although it makes a connection ( i see it with netstat -n ) it doesnt dump the source of index.html to my screen....

    heres the code:
    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdio.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    
    int main( int argc, char **argv )
    {
            if( argc < 2 )
            {
                    printf("Usage: httprq <host>\n");
                    exit(1);
            }
    
            struct hostent *h;
            struct sockaddr_in dest;
            int sockfd;
    
            if( (h = (struct hostent *)gethostbyname(argv[1])) == -1 )
            {
                    printf("Could not lookup hostname\n");
                    exit(1);
            }
    
            if( (sockfd = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
            {
                    printf("Could not create socket\n");
                    exit(1);
            }
    
            dest.sin_port = htons(80);
            dest.sin_family = AF_INET;
            dest.sin_addr = *((struct in_addr *)h->h_addr);
    char *msg = "GET /index.html HTTP/1.1";
            char recv[900];
    
            if( (connect( sockfd, (struct sockaddr *)&dest, sizeof(struct sockaddr))) == -1 )
            {
                    printf("Could not connect to remote host\n");
                    close(sockfd);
            }
            else
            {
                    write( sockfd, msg, strlen(msg) );
                    read( sockfd, recv, 900 );
                    printf("%s\n", recv );
                    close(sockfd);
            }
    
            exit(1);
    }
    It just doesnt do anything, i didnt wait for that long because i know that other similair programs returned the source in a few seconds...
    btw; i also tried it with send() and recv() no luck either....

    thanks,
    encrypted

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    24
    Ok thanks,

    i read beejs guide partly, but ill read the whole again.

    Anyway, so the return value of the read and write call has to be checked.
    And i have to read/write in a loop.

    but is the other piece of the code correct besides read and write?

    thanks,
    encrypted

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char *msg = "GET /index.html HTTP/1.1";
    I seem to remember that the request must be terminated with carriage return/line feed. Better read the RFC if you want to do this properly

    >>4. Can you read/write to the same socket?
    Yes (providing it opened successfully of course).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User devil@work's Avatar
    Join Date
    Mar 2003
    Posts
    33
    i just had the same question you have to add a \n to the end search my treads same question answered

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my HTTP request handler code correct?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-25-2008, 04:01 AM
  2. HTTP GET and POST REQUEST
    By strickey in forum Networking/Device Communication
    Replies: 3
    Last Post: 04-20-2007, 04:23 PM
  3. Sending an http POST request
    By jaxen in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2006, 11:35 AM
  4. sending https request
    By sreenu_daram in forum Networking/Device Communication
    Replies: 1
    Last Post: 08-05-2006, 09:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21