1

HTTP 1.1 の使用法を読んだ後、この質問をすることにしました。www.google.com にもアクセスできない理由がわかりません。毎回3xxから4xx(ほぼすべてのエラー)のエラーが発生します。HTTP/1.1 でこれらの GET HOST CONNECT のすべての組み合わせを試しましたが、まだ理解できません。接続が安全でないと拒否/スロー/想定されたのはなぜですか?

#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

注: どこでも答えが見つかったら追加します。

4

1 に答える 1

1

仕様を誤解しています。

リクエスト ペイロードの最初の行には、絶対 URI またはパスを含める必要があります。ホスト名ではありません。
ドメイン名はHost:ヘッダーに入ります。

詳細については、仕様を参照してください。

于 2013-08-30T02:57:13.103 に答える