gpt4 book ai didi

C语言,获取HTML源码

转载 作者:可可西里 更新时间:2023-11-01 02:57:50 24 4
gpt4 key购买 nike

我正在尝试获取此页面的 HTML http://pastebin.com/raw/7y7MWssc使用 C。到目前为止,我正在尝试使用套接字和端口 80 连接到 pastebin,然后使用 HTTP 请求获取该 pastebin 页面上的 HTML。

我知道到目前为止我所拥有的可能还有很长的路要走,但这里是:

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

int main()
{
/*Define socket variables */
char host[1024] = "pastebin.com";
char url[1024] = "/raw/7y7MWssc";
char request[2000];
struct hostent *server;
struct sockaddr_in serverAddr;
int portno = 80;

printf("Trying to get source of pastebin.com/raw/7y7MWssc ...\n");

/* Create socket */
int tcpSocket = socket(AF_INET, SOCK_STREAM, 0);
if(tcpSocket < 0) {
printf("ERROR opening socket\n");
} else {
printf("Socket opened successfully.\n");
}

server = gethostbyname(host);
serverAddr.sin_port = htons(portno);
if(connect(tcpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0) {
printf("Can't connect\n");
} else {
printf("Connected successfully\n");
}

bzero(request, 2000);
sprintf(request, "Get %s HTTP/1.1\r\n Host: %s\r\n \r\n \r\n", url, host);
printf("\n%s", request);

if(send(tcpSocket, request, strlen(request), 0) < 0) {
printf("Error with send()");
} else {
printf("Successfully sent html fetch request");
}
printf("test\n");

}

上面的代码在某种程度上是有意义的,现在我很困惑。我将如何使它从 http://pastebin.com/raw/7y7MWssc 获取网络资源?

最佳答案

已修复,我需要设置添加

serverAddr.sin_family = AF_INET;

和 bzero serverAddr,还有我的 HTTP 请求是错误的,它有一个额外的/r/n 和空格,就像@immibis 说的。

更正:

sprintf(request, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", url, host);

关于C语言,获取HTML源码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38648012/

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