gpt4 book ai didi

c - HTTP 请求被拒绝(3xx 4xx 响应)

转载 作者:太空宇宙 更新时间:2023-11-04 03:54:00 25 4
gpt4 key购买 nike

我决定在阅读 HTTP 1.1 用法后问这个问题。我不明白为什么我什至无法访问 www.google.com。它每次都会给出 3xx 到 4xx(几乎所有错误)错误。我已经在 HTTP/1.1 中尝试了这些 GET HOST CONNECT thingys 的所有组合,但仍然无法理解它。为什么我的连接被拒绝/抛出/假定为不安全?

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>

#define TRUE 1
#define FALSE 0

#define INT_MAX_VAL 100000000
#define MILL 1000000

#define DEF_PORT "80"

typedef enum compOpts
{
higher,
lower,
hEqual,
lEqual,
equal,
nEqual
} compOpts_t;

void checkErr(char *title ,int status ,int trueVal ,compOpts_t compType ,char* (*errFunc)(int errCode));

int main()
{
int status,
servSockFD,
byteCount = 0 ;
struct addrinfo hints ,*servinfo;
struct sockaddr_in *servSock;
char *ip4,
*message = "GET www.google.com HTTP/1.1\r\n\r\n",
*htmlCode;

/**initialization*/

memset(&hints ,0 ,sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;

ip4 = (char *)malloc(sizeof(char)*INET_ADDRSTRLEN);
htmlCode = (char *)malloc(sizeof(char)*INT_MAX_VAL);

/**getting server info's*/
status = getaddrinfo("www.google.com" ,DEF_PORT ,&hints ,&servinfo);
checkErr("serv info" ,status ,0 ,equal ,gai_strerror);

/**Checking ipAddress*/
servSock = (struct sockaddr_in *)servinfo->ai_addr;

status = inet_ntop(AF_INET ,&servSock->sin_addr ,ip4,INET_ADDRSTRLEN);
checkErr("ntop" ,status ,0 ,nEqual ,gai_strerror);

printf("IPv4 : %s\n",ip4);

/**creating socket*/
servSockFD = socket(servinfo->ai_family ,servinfo->ai_socktype ,servinfo->ai_protocol);
if (servSockFD == -1)
{
status = errno;
checkErr("socket creation" ,status ,status ,nEqual ,strerror);
}

/**connecting through socket*/
status = connect(servSockFD ,servSock ,servinfo->ai_addrlen);
if(status == -1)
{
status = errno;
checkErr("connection" ,status ,status ,nEqual ,strerror);
}

/**sending through socket*/
do{
byteCount = send(servSockFD ,message ,strlen(message) ,0);
if(byteCount==-1)
{
status = errno;
checkErr("send" ,status ,status ,nEqual ,strerror);
break;
}
}while(byteCount!=strlen(message));

/**recieving html code from socket*/
byteCount = recv(servSockFD ,htmlCode ,INT_MAX_VAL ,0);
if(byteCount == -1)
{
status = errno;
checkErr("recv" ,status ,status ,nEqual ,strerror);
}

printf("%s",htmlCode);

free(ip4);
freeaddrinfo(servinfo);
return 0;
}

void checkErr(char *title ,int status ,int trueVal ,compOpts_t compType ,char* (*errFunc)(int errCode))
{
int res=TRUE;

switch (compType)
{
case hEqual:
if(status != trueVal)
res =FALSE;
case higher:
if(res == TRUE && status<trueVal)
res=FALSE;
break;
case lEqual:
if(status!=trueVal)
res=FALSE;
case lower:
if(status>trueVal)
res=FALSE;
break;
case equal:
if(status!=trueVal)
res=FALSE;
break;
case nEqual:
if(status==trueVal)
res=FALSE;
break;
default:
printf("Error : Unknown comparision type.\n");
break;
}

if(res==TRUE)
printf("%s : succeeded.\n" ,title);
else
printf("%s : failed.\nError Message : %s\n" ,title,errFunc(status));
}

实际上我想获取任何使用 HTTP/1.1 的站点的 html 代码。指导我,哪里错了,我应该怎么做?我在这个代码上坚持了 6 个小时 :S

注意:如果我能在任何地方找到它的答案,我会添加。

最佳答案

您误解了规范。

请求有效载荷的第一行应包含绝对 URI 或路径;不是主机名。
域名位于 Host: header 中。

有关详细信息,请参阅 spec .

关于c - HTTP 请求被拒绝(3xx 4xx 响应),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18524190/

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