在阅读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
注意:如果我可以在任何地方找到答案,我会添加。