简体   繁体   English

在 C 中发出 Get HTTP 请求

[英]Making a Get HTTP request in C

I'm trying to make a C file that retrieves a web page using HTTP Protocol.我正在尝试制作一个 C 文件,该文件使用 HTTP 协议检索 web 页面。 The GET request accepts 3 arguments, the hostname, the web page, and the port number. GET 请求接受 3 arguments、主机名、web 页面和端口号。

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char* argv[]){

    if(argc < 4){
        fprintf(stderr, "usage %s [hostname][filepath][port#]", argv[0]);
        exit(0);
    }

    int byte_info;
    int sockfd;
    struct hostent *host_server;
    struct sockaddr_in server_addr;
    char buffer [256];
    char *asprint_str;
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    host_server = gethostbyname(argv[1]);
    if(host_server == NULL)
    {
        fprintf(stderr, "Server not found\n\n");
        exit(0);
    }
    bzero((char*) &server_addr, sizeof(server_addr));
    server_addr.sin_port = htons(atoi(argv[3]));
    server_addr.sin_family = AF_INET;
    bcopy((char*) host_server->h_addr, (char*) &server_addr.sin_addr.s_addr, sizeof(server_addr));

    if (sockfd < 0)
        error("Error opening socket");
    if(connect(sockfd, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0)
        error("Couldn't connect");
    bzero(buffer, 256);
    asprintf(&asprint_str, "GET %s HTTP/1.1\r\nHOST: %s\r\n\r\n", argv[2], argv[3]);
    printf("%s", asprint_str);

    byte_info = write(sockfd, asprint_str, strlen(asprint_str));

    if (byte_info < 0)
        error("Write to socket failed");
    else
    {
        printf("HTTP Resuest sent to host server\n\n");

        while(byte_info > 0)
        {
            bzero(buffer, 256);
            byte_info = read(sockfd, buffer, 255);
            if (byte_info > 0)
                printf("%s", buffer);
        }
    
    }
close(sockfd);   

}

And every time I try to compile I get this error:每次我尝试编译时都会收到此错误:

$ gcc -Wall -g -D_GNU_SOURCE -o Get Get.c
Get.c: In function ‘main’:
Get.c:45:5: warning: ‘__builtin_memmove’ writing 16 bytes into a region of size 12 overflows the 
destination [-Wstringop-overflow=]
45 |     bcopy((char*) host_server->h_addr, (char*) &server_addr.sin_addr.s_addr, sizeof(server_addr));
  |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I'm new to C and I'm trying to understand what that error means and what would I need to do to fix it.我是 C 的新手,我正在尝试了解该错误的含义以及我需要做些什么来修复它。

Reason for warning : You are writing more bytes (4 bytes extra) to host_server->h_addr , which causes overflow.警告原因:您正在向host_server->h_addr写入更多字节(额外 4 个字节),这会导致溢出。 You can only write host_server->h_length bytes.您只能写入host_server->h_length字节。

Solution:解决方案:

change改变

bcopy((char*) host_server->h_addr, (char*) &server_addr.sin_addr.s_addr, sizeof(server_addr));

to

bcopy((char*) host_server->h_addr, (char*) &server_addr.sin_addr.s_addr, host_server->h_length);

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM