0

Here is my code where I tried to make an HTTP request

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>


int main(int argc, char* argv[]) {
 //create the socket
 int network_socket;
 network_socket = socket(AF_INET, SOCK_STREAM, 0);

 //specify an address for the socket
 struct sockaddr_in server_address;
 server_address.sin_family = AF_INET;
 server_address.sin_port = htons(80);
 inet_pton(AF_INET, "208.97.177.124", &server_address.sin_addr);


 //connect
 int connection_status = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
 if (connection_status == -1) {
  printf("There was an error making a connection to the remote socket\n\n");
 }

 //send information
 char message[1024];
 strcpy(message , "GET /index.html HTTP/1.1\r\nHost: perdu.com\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)\r\nAccept-Language: en-us\r\nAccept-Encoding: gzip, deflate\r\nConnection: Keep-Alive\r\n\r\n");

 send(network_socket, message, sizeof(message), 0);

 //receive information
 char server_response[2048];
 memset(server_response, '0',sizeof(server_response));
 int size = recv(network_socket, &server_response, sizeof(server_response), 0);

 //print the size
 printf("size is %d\n\n", size);

 //print the data
 printf("The server sent the response : %s\n", server_response);

 //close the socket
 close(network_socket);

 return 0;
}

The server sent the response :

HTTP/1.1 200 OK
Date: Wed, 25 Mar 2020 14:29:19 GMT
Server: Apache
Upgrade: h2
Connection: Upgrade, Keep-Alive
Last-Modified: Thu, 02 Jun 2016 06:01:08 GMT
ETag: "cc-5344555136fe9-gzip"
Accept-Ranges: bytes
Cache-Control: max-age=600
Expires: Wed, 25 Mar 2020 14:39:19 GMT
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Length: 163
Keep-Alive: timeout=2, max=100
Content-Type: text/html

?

And it ends with a question mark "?" instead of the HTML content. Why ?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1. You may need to make multiple calls to `recv` to get all the data. 2. You need to terminate the text with `\0`. – 001 Mar 25 '20 at 15:03
  • How to do that ? And why call recv multiple times ? – fffgggffggf Mar 25 '20 at 15:05
  • [C++ Socket recv not receiving correct number of bytes?](//stackoverflow.com/a/27536124) (Ignore the C++ tag. It is still applicable) – 001 Mar 25 '20 at 15:08
  • I tried, same error... – fffgggffggf Mar 25 '20 at 15:18
  • There are MANY problems with this code. Ignoring the return value of `send()` and not calling it in a loop until the full request is sent. Ignoring the return value of `recv()` and not calling it in a loop until the full response is received. `printf()`'ing data that is not null terminated. Not parsing the response headers to know the format of the body data. In particular, the server is sending `Content-Encoding: gzip` which means the body is **compressed**, because you sent `Accept-Encoding: gzip, deflate` to allow compression. Get rid of that header and you should see the HTML text. – Remy Lebeau Mar 25 '20 at 23:33
  • See [this answer](https://stackoverflow.com/a/30472253/65863) and [this answer](https://stackoverflow.com/a/16247097/65863) for more details about the proper way to handle an HTTP response. – Remy Lebeau Mar 25 '20 at 23:38

0 Answers0