zoukankan      html  css  js  c++  java
  • Linux下使用http协议下载文件

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <arpa/inet.h>
    #include <sys/socket.h>
    
    //下载目标文件 : http://ftp.gnu.org/gnu/bc/bc-1.03.tar.gz
    #define HOST_ADDR "ftp.gnu.org"
    #define FILE_PATH "/gnu/bc/bc-1.03.tar.gz"
    #define FILE_NAME "bc-1.03.tar.gz"
    
    void write_to_socket(int socketfd, const char* str)
    {
        write(socketfd, str, strlen(str));
    }
    
    int main(void)
    {
        char* pstr = NULL;
        int sk_fd = 0;
        int size = 0;
        int sum = 0;
        unsigned long filesize = 0;
        FILE* fp = NULL;
        FILE* newfp = NULL;
        struct sockaddr_in sk_addr;
        struct hostent *phost = NULL;
        char buf[1024] = {0};
        float processbar = 0.0f;
        
        // create TCP socket
        sk_fd = socket(AF_INET, SOCK_STREAM, 0);
        if (sk_fd < 0)
        {
            perror("socket failed");
            exit(1);
        }
        
     
        phost = gethostbyname(HOST_ADDR);
        
        memset(&sk_addr, 0x00, sizeof(sk_addr));
        sk_addr.sin_family = AF_INET;
        sk_addr.sin_port = htons(80);
        sk_addr.sin_addr.s_addr = ((struct in_addr *)phost->h_addr)->s_addr;
        if (connect(sk_fd, (struct sockaddr *)&sk_addr, sizeof(sk_addr)) < 0)
        {
            perror("connect failed");
            close(sk_fd);
            exit(1);
        }
        
        write_to_socket(sk_fd, "GET "FILE_PATH" HTTP/1.1
    ");
        write_to_socket(sk_fd, "Accept: */*
    ");
        write_to_socket(sk_fd, "Host: "HOST_ADDR"
    ");
        write_to_socket(sk_fd, "Connection: Keep-Alive
    ");
        write_to_socket(sk_fd, "
    ");
        shutdown(sk_fd, SHUT_WR);    
    
        fp = fdopen(sk_fd, "r");
        newfp = fopen(FILE_NAME, "w+");
        memset(buf, 0x00, sizeof(buf));
        while (fgets(buf, 1023, fp))
        {
            printf("%s
    ", buf);
            
            if (filesize != 0) /* find CRLF */
            {
                if (2 == strlen(buf))
                {
                    break;
                }
            }
            else /* get file size */
            {
                pstr = strstr(buf, "Content-Length:");
                if (pstr != NULL)
                {
                    pstr = strchr(pstr, ':');
                    pstr++;
                    filesize = strtoul(pstr, NULL, 10);
                    printf("filesize = %lu
    ", filesize);
                }
            }
            memset(buf, 0x00, sizeof(buf));
        }
        
        while (1)
        {
            size = fread(buf, 1, 1024, fp);
            sum += size;
            if (0 == size)
            {
                break;
            }
            size = fwrite(buf, 1, size, newfp);
            processbar = (float)sum / (float)filesize;
            printf("%6d/%lu[%2.1f]
    ", sum, filesize, processbar * 100);
        }
        fclose(fp);
        fclose(newfp);
        
        return 0;
    }
  • 相关阅读:
    Matrix Chain Multiplication[HDU1082]
    Color a Tree[HDU1055]
    Piggy-Bank[HDU1114]
    Catching Fish[HDU1077]
    用单位圆覆盖尽量多的点
    LianLianKan[HDU4272]
    Travelling
    求多边形面积
    方格取数(1)
    Pebbles
  • 原文地址:https://www.cnblogs.com/LubinLew/p/Linux-Http-DownLoad.html
Copyright © 2011-2022 走看看