Вот мой код, где я пытался сделать HTTP-запрос
#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;
}
Сервер отправил ответ:
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
?
И он заканчивается знаком вопроса "?" вместо контента HTML. Почему?