ubuntu系统,准备实现一个http请求小程序
程序名为client,运行格式为: ./client host port 如:./client http://www.baidu.com/ 80
分别尝试了向baidu.com以及sina.com.cn发请求,
前者返回状态为302,但用浏览器打开返回的html格局相当混乱;
返回结果节选:
112.80.248.75
Send
Get / HTTP/1.1
Host: www.baidu.com
User-Agent: curl/7.68.0
Connection: close
Accept: */*
Request Sended......
Waitting for Response...
HTTP/1.1 302 Found
Content-Length: 17931
Content-Type: text/html
Date: Sun, 31 Jul 2022 17:15:48 GMT
Etag: "54d9749e-460b"
Server: bfe/1.0.8.18
Connection: close
而后者始终返回400
返回结果:
101.226.26.243
Send
Get / HTTP/1.1
Host: www.sina.com.cn
User-Agent: curl/7.68.0
Connection: close
Accept: */*
Request Sended......
Waitting for Response...
HTTP/1.1 400 Bad Request
Server: Tengine
Date: Sun, 31 Jul 2022 17:33:04 GMT
Content-Type: text/html
Content-Length: 249
Connection: close
Via: vcache8.cn3775[,0]
Timing-Allow-Origin: *
EagleId: 0000000016592887843148738e
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>400 Bad Request</title></head>
<body>
<h1>400 Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<hr/>Powered by Tengine</body>
</html>
HTTP/1.1 400 Bad Request
Server: Tengine
Date: Sun, 31 Jul 2022 17:33:04 GMT
Content-Type: text/html
Content-Length: 249
Connection: close
Via: vcache8.cn3775[,0]
Timing-Allow-Origin: *
EagleId: 0000000016592887843148738e
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>400 Bad Request</title></head>
<body>
<h1>400 Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<hr/>Powered by Tengine</body>
</html>
iret = 0,No Messages
All Recvd
与之相对,curl -v 命令总是可以得到200的正常返回状态
我想知道是什么原因造成了这样的输出结果,以及如何改正才能使返回码为正常的200
#include <stdio.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<netdb.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<signal.h>
int sockfd;
void EXIT(int sig)
{
printf("\nProgram Close\n");
close(sockfd);
exit(-1);
}
void setHead(char *buff,char *addr)
{
memset(buff,0,sizeof(buff));
strcat(buff, "Get / HTTP/1.1\r\n");
strcat(buff, "Host: ");
strcat(buff, addr);
strcat(buff,"\r\nUser-Agent: curl/7.68.0\r\n");
strcat(buff,"Connection: close\r\n");
strcat(buff,"Accept: */*\r\n\r\n");
return;
}
int main(int argc , char** argv)
{
signal(SIGINT,EXIT);
signal(15,EXIT);
if(argc != 3)
{
printf("./client addr port\nExample:./client 127.0.0.1 5000\n");
return -1;
}
//Socket Create
if( (sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0 )
{
perror("socket");
return -1;
}
//Get Server Address
struct hostent* h;
if ( (h = gethostbyname(argv[1])) == NULL )
{
perror("gethostbyname");
close(sockfd);
return -1;
}
//Socket Connect
struct sockaddr_in servaddr;
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(atoi(argv[2]));
memcpy(&servaddr.sin_addr,h->h_addr,h->h_length);
printf("%s\n",inet_ntoa(servaddr.sin_addr));
if( connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)) != 0)
{
perror("connect");
close(sockfd);
return -1;
}
//Http Message Initialized
char buffer[1024];
int iret;
memset(buffer,0,sizeof(buffer));
setHead(buffer,argv[1]);
//Http Send & Recv
if( (iret = send(sockfd,buffer,strlen(buffer),0)) <= 0)
{
perror("send");
close(sockfd);
return -1;
}
printf("Send\n %s\n\n",buffer);
printf("Request Sended......\nWaitting for Response...\n");
memset(buffer,0,sizeof(buffer));
while(1)
{
if(( iret = recv(sockfd,buffer,sizeof(buffer),0)) <= 0)
{
printf("iret = %d,No Messages\n",iret);
break;
}
printf("%s",buffer);
fwrite(buffer,1,iret,stdout);
}
printf("All Recvd\n");
close(sockfd);
return 0;
}
302 Found
请求的资源现在临时从不同的 URI 响应请求。由于这样的重定向是临时的,客户端应当继续向原有地址发送以后的请求。只有在Cache-Control或Expires中进行了指定的情况下,这个响应才是可缓存的。
参考链接:https://blog.csdn.net/independe/article/details/97686555
302(临时移动) 服务器目前正从不同位置的网页响应请求,但请求者应继续使用原有位置来进行以后的请求。此代码与响应 GET 和 HEAD 请求的 301 代码类似,会自动将请求者转到不同的位置。但由于 Googlebot 会继续抓取原有位置并将其编入索引,因此您不应使用此代码来通知 Googlebot 某个页面或网站已被移动。
400(错误请求) 服务器不理解请求的语法。