gpt4 book ai didi

c - 我无法获得完整网页

转载 作者:太空宇宙 更新时间:2023-11-04 06:15:04 24 4
gpt4 key购买 nike

我想做 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;
}

执行:

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

最佳答案

正如评论中所指出的,您必须重复recv 直到返回值小于或等于零。同样在这种简单的情况下,我建议发送 HTML 1.0 请求以避免答案分块。试试下面的代码:

#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;
}

关于c - 我无法获得完整网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47679092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com