我无法获得完整的网页

时间:2017-12-06 16:30:09

标签: c http

我想做http请求,但它不会下载整页。任何大型网页只有7300字节或更少 如果我尝试小网页,它下载完整 我尝试了不同的旗帜,但仍然无法找到我的错误

#define POSIX_C_SOURCE >= 200112L
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
struct addrinfo hints, *res;
int sockfd;
char buf[25056];
int byte_count;

 memset(&hints, 0,sizeof hints);
hints.ai_family=AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
getaddrinfo("man7.org","80", &hints, &res);
sockfd = socket(res->ai_family,res->ai_socktype,res->ai_protocol);
printf("Connecting...\n");
connect(sockfd,res->ai_addr,res->ai_addrlen);
printf("Connected!\n");
char *header = "GET /linux/man-pages/man2/recv.2.html HTTP/1.1\r\nHost:         
man7.org\r\n\r\n";
send(sockfd,header,strlen(header),0);
printf("GET Sent...\n");
//all right ! now that we're connected, we can receive some data!

byte_count = recv(sockfd,buf,sizeof(buf),0);
printf("recv()'d %d bytes of data in buf\n",byte_count);
printf("%.*s",byte_count,buf); // <-- give printf() the actual data size

return 0;
}

exec:

Connecting...
Connected!
GET Sent...
recv()'d 7300 bytes of data in buf     
HTTP/1.1 200 OK
Date: Wed, 06 Dec 2017 16:04:29 GMT
Server: Apache
Last-Modified: Tue, 05 Dec 2017 18:41:34 GMT
ETag: "4d77-64f1-55f9c2f245380"
Accept-Ranges: bytes
Content-Length: 25841
Connection: close
Content-Type: text/html; charset=UTF-8

1 个答案:

答案 0 :(得分:4)

正如评论中所指出的,您必须重复#define POSIX_C_SOURCE >= 200112L #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <unistd.h> #include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char* argv[]) { struct addrinfo hints, *res; int sockfd; char buf[65536]; int n, byte_count; memset(&hints, 0,sizeof hints); hints.ai_family=AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; getaddrinfo("man7.org","80", &hints, &res); sockfd = socket(res->ai_family,res->ai_socktype,res->ai_protocol); printf("Connecting...\n"); connect(sockfd,res->ai_addr,res->ai_addrlen); printf("Connected!\n"); char *header = "GET /linux/man-pages/man2/recv.2.html HTTP/1.0\r\nHost:man7.org\r\n\r\n"; send(sockfd,header,strlen(header),0); printf("GET Sent...\n"); //all right ! now that we're connected, we can receive some data! byte_count = n = 0; do { byte_count += n; n = recv(sockfd,buf+byte_count,sizeof(buf)-byte_count,0); } while (n > 0); printf("recv()'d %d bytes of data in buf\n",byte_count); printf("%.*s",byte_count,buf); // <-- give printf() the actual data size return 0; } ,直到返回的值小于或等于零。同样在这种简单的情况下,我建议发送HTML 1.0请求以避免分块答案。请尝试以下代码:

{{1}}