C 从HTTP响应中获取图片

C++ get picture from HTTP response

本文关键字:获取 取图片 响应 HTTP      更新时间:2023-10-16

我正在尝试从网站下载图片。问题在于,即使我从HTTP响应主体中获得所有内容,文件也不会打开。我一直在尝试解决这个问题,但找不到真正的问题。我注意到的一件事是,使用铬显示的图片显示不同的字符与使用命令从我的代码下载的图片: $ 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, "rnrn");
    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.1rnhost: www.pngimg.comrnConnection: closernrn";
    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命令的图片:上面的终端是我的代码中的图片,下面是Chromium"另存为"

"的图片

问题是,我不知道在C样式字符串中您无法在二进制数据上进行strlen。这就是为什么我必须在函数中添加计数器removehttpheader的原因。以下是我更改的函数getPicture和removehttpheader。

char *removeHTTPHeader(char *buffer, int &bodySize) {
    char *t = strstr(buffer, "rnrn");
    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();
}