1
HTTP 1.1 사용법을 읽은 후에이 질문을하기로했습니다. 왜 내가 www.google.com에 액세스 할 수 없는지 모르겠다. 매 3xx에서 4xx (거의 모든 오류) 오류가 발생합니다. HTTP/1.1에서이 GET HOST CONNECT의 모든 조합을 시도했지만 아직 이해할 수는 없습니다. 왜 내 연결이 거부/던지기/안전하지 않은 것으로 가정 했습니까?HTTP 요청이 거부되었습니다 (3xx 4xx 응답)
#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 .Guide 나, 어디 잘못된 일이며, 내가 무엇을해야합니까를 사용하는 모든 사이트의 HTML 코드를 얻으려면? 나는이 코드를 6 시간 동안 지속했다 : S
참고 : 어디에서나 대답을 찾을 수 있다면 추가 할 것이다.
[여기]에서이 사용법을 보았습니다. (http://greenbytes.de/tech/webdav/draft-ietf-httpbis-p1-messaging-latest.html) 나는 모든 조합을 시도했습니다. ss] (http://i.imgur.com/HMh0tB6.png) 왜 302 리디렉션을 사용하고 있습니까? 난 그냥 Google의 맨 페이지를 요청 & 아직도 리디렉션있어? –
구글이 원하는 것 때문에 리디렉션되고 있습니다. 그게 전부입니다. www.google.com으로 이동하여 '/'을 요청하면 Google에서 Set-Cookie 및 리디렉션을 제공합니다. 이것을 이해하려고 애 쓰지 마십시오. 그것은 그들이 사물을 구현하기로 결정한 것입니다. 원하는 경우 쿠키를 가져 와서 리디렉션을 따라 가면서 얻은 결과를 볼 수 있지만 적어도 302를 얻는 것은 요청을 성공적으로 제출했음을 의미합니다. –