C 从HTTP响应中获取图片
C++ get picture from HTTP response
我正在尝试从网站下载图片。问题在于,即使我从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();
}
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 从两个 4x64 位整数数组中获取取模
- 如何获取C++中的输入并将其"split"到列表中?换句话说,取 N 个输入并放入 N 长度的数组中
- (C++) 取字符串的每个字符并将其放入数组中并获取所述数组的大小?
- 从参考中获取的指针可以在定义明确的C 中取无效
- 在程序中获取 USB 相机图片
- C 从HTTP响应中获取图片
- OpenCV-从C/C++中的默认网络摄像头获取图片-GTK问题
- 获取 mp3 的专辑封面图片的地址
- 如何从QWebView获取图片
- 当华硕Xtion Pro插入时,无法使用OpenCV的网络摄像头获取图片
- 如何获取点向量并仅抓取这些点的"y"
- 获取图片消息到c++程序
- 使用Opencv获取多张图片的标准偏差
- 如何从AVFrame中获取亮度图片