1

I have trying to make a GET/ POST request. GET request for now. I am able to establish connection but the response i get is never 200 OK. Here is the code

   int socketFD;
    struct sockaddr_in myaddr;
    //    struct sockaddr_in hints;
    socketFD = socket(AF_INET, SOCK_STREAM, 0);
    if (socketFD == -1) {
        NSLog(@"Error in making socket");
    }      
    myaddr.sin_port = htons(80);
    memset(&myaddr.sin_zero, '\0', 8);


    char *sendbug = "GET 173.194.66.99 HTTP/1.0\r\n\r\n";


    struct sockaddr_in dest,their_addr;
    //    struct addrinfo *res;
    struct sockaddr sockAddrToDisplay;

    dest.sin_family = AF_INET;
    dest.sin_port = htons(80);
    NSLog(@"%s",sendbug);
    dest.sin_addr.s_addr = inet_addr("173.194.66.99"); // IP for google


    int connectResult =    connect(socketFD, (struct sockaddr *)&dest, sizeof(struct sockaddr));

    if (connectResult == -1) {
        NSLog(@"Error connecting");
    }

    int sendResult =   sendto(socketFD, sendbug, strlen(sendbug), 0, (struct sockaddr *)&dest, sizeof(struct sockaddr));

    if (sendResult == -1) {
        NSLog(@"Sendind failed");
    }

    //    int peerResult =    getpeername(socketFD, (struct sockaddr *)&dest,(socklen_t *) sizeof(struct sockaddr));


    struct addrinfo hints, *res;
    int sockfd;
    int byte_count;
    socklen_t fromlen;
    struct sockaddr_storage addr;
    char buf[512];
    char ipstr[INET6_ADDRSTRLEN];


    fromlen = sizeof addr;
    byte_count = recvfrom(socketFD, buf, sizeof buf, 0,(struct sockaddr *) &addr, &fromlen);

    NSLog(@"%s",buf);

And the response. I need some help where i am doing wrong and also, how i can make a http POST request. thx in advance fellows

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.3.7</center>
</body>
</html>
∑¥ëXnr
Azerue
  • 258
  • 6
  • 16
  • Quite an ambitious project: +1. Niels offered a good advice: standards & recommendations. You can test his answer simply by _telneting_ to your server (on port 80) and issuing a `GET /` command. – Rok Jarc Apr 29 '13 at 10:54

1 Answers1

4

Your problem is here:

char *sendbug = "GET 173.194.66.99 HTTP/1.0\r\n\r\n";

HTTP protocol defines a request as <method> <resource> [HTTP/<version>]. Version defaults to 1.0, and in 1.0 you only need one newline at the end, and the resource you're requesting is the homepage, so this should be:

char *sendbug = "GET /\r\n";

I'd recommend reading up on the HTTP-RFC's if you're trying to implement a custom client, that should also help you in constructing a POST-request. And please upgrade to HTTP/1.1 while at it, not all webservers feel obliged to support 1.0 anymore.

RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
  • You probably also want a `Host:` header. – Mike Weller Apr 29 '13 at 11:01
  • Thx for replying....i tried char *sendbug = "GET 173.194.66.99 HTTP/1.0\r\n" ; like you said but still the response is same. And HTTP/1.1 headers looked a bit complicated so i was trying to send request with 1.0. – Azerue Apr 29 '13 at 11:03
  • 1
    @MikeWeller `Host` header only exists in HTTP/1.1, not relevant for a 1.0 request. – Niels Keurentjes Apr 29 '13 at 11:10
  • @Azerue If you thank me for the answer at least try to read it, you're still sending the IP address as the resource instead of `/` as you should. – Niels Keurentjes Apr 29 '13 at 11:11
  • thank you fellows i actually got 200 OK response using char *sendbug = "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n"; – Azerue Apr 29 '13 at 11:22