我正在尝试从网站下载图片。问题是,即使我从HTTP响应正文中获取了所有内容,文件也无法打开。我一直在尝试解决此问题,但找不到真正的问题。我注意到的一件事是,使用 Chrome 下载的图片显示的字符与使用命令从我的代码下载的图片显示的字符不同:
$ cat picture.png |少

#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fstream>
#include <iostream>

using std::cout;
using std::endl;

//with this function I remove HTTP header info, so I only get content.
char *removeHTTPHeader(char *buffer) {
    char *t = strstr(buffer, "\r\n\r\n");
    t = t + 4;
    return t;
}

void getPicture(const int &socketfd, const int &bSize) {
    std::ofstream file("picture.png",
            std::ofstream::binary | std::ofstream::out);

    char buffer[bSize];
    ssize_t bReceived;

    bReceived = recv(socketfd, buffer, bSize, 0);
    char *t = removeHTTPHeader(buffer);
    file.write(t, strlen(t));
    memset(buffer, 0, bSize);

    while ((bReceived = recv(socketfd, buffer, bSize, 0)) > 0) {
        file.write(buffer, bReceived);
        memset(buffer, 0, bSize);
    }

    file.close();
}

int main() {

    int status;
    addrinfo host_info;
    addrinfo *host_info_list;

    memset(&host_info, 0, sizeof(host_info));

    host_info.ai_family = AF_UNSPEC;
    host_info.ai_socktype = SOCK_STREAM;
    host_info.ai_protocol = 0;

    status = getaddrinfo("www.pngimg.com", "80", &host_info, &host_info_list);
    if (status != 0) {
        cout << "getaddrinfo error" << endl;
    }

    int socketfd;
    socketfd = socket(host_info_list->ai_family, host_info_list->ai_socktype,
            host_info_list->ai_protocol);

    addrinfo *rp;
    for (rp = host_info_list; rp != NULL; rp = rp->ai_next) {

        socketfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);

        if (socketfd == -1) {
            cout << "socket error" << endl;
        }

        if (connect(socketfd, rp->ai_addr, rp->ai_addrlen) != -1) {
            break;
        }
        close(socketfd);
    }

    if (rp == NULL) {
        cout << "Could not connect!" << endl;
        exit(EXIT_FAILURE);
    }

    freeaddrinfo(host_info_list);

    const char *msg =
            "GET /upload/water_PNG3290.png HTTP/1.1\r\nhost: www.pngimg.com\r\nConnection: close\r\n\r\n";

    status = send(socketfd, msg, strlen(msg), 0);

    if (status == -1) {
        cout << "error sending" << endl;
        exit(EXIT_FAILURE);
    }

    getPicture(socketfd, 1024);
    close(socketfd);

    return 0;
}

这是使用cat命令的图片:
terminal above is picture from my code, below is picture from chromium "save as"

最佳答案

问题是我不知道在C样式字符串中不能对二进制数据感到吃惊。这就是为什么我必须在removeHTTPHeader函数中添加计数器。下面是我更改过的函数getPicture和removeHTTPHeader。

char *removeHTTPHeader(char *buffer, int &bodySize) {
    char *t = strstr(buffer, "\r\n\r\n");
    t = t + 4;

    for (auto it = buffer; it != t; ++it) {
        ++bodySize;
    }

    return t;
}

void getPicture(const int &socketfd, const int &bSize) {
    std::ofstream file("picture.png",
            std::ofstream::binary | std::ofstream::out);

    char buffer[bSize];
    ssize_t bReceived;

    bReceived = recv(socketfd, buffer, bSize, 0);
    int bodySize = 0;

    char *t = removeHTTPHeader(buffer, bodySize);
    bodySize = bReceived - bodySize;

    file.write(t, bodySize);
    memset(buffer, 0, bSize);

    while ((bReceived = recv(socketfd, buffer, bSize, 0)) > 0) {
        file.write(buffer, bReceived);
        memset(buffer, 0, bSize);
    }

    file.close();
}

关于c++ - C++从HTTP响应获取图片,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37985610/

10-15 04:31