使用http get下载图片只会下载其中的一小部分
Downloading a picture with http get only downloads a small part of it
我正在尝试使用http协议GET请求服务器向我发送图片。它会编译并询问图片,但服务器只会向我发送一小部分
我的代码是:
#include <iostream>
#include <string>
#include <Windows.h>
#include <fstream>
#include <stdlib.h>
#include <winsock.h>
#pragma comment(lib, "wsock32.lib")
using namespace std;
#define BUFFERSIZE 1024
void die_with_error(char *errorMessage);
void die_with_wserror(char *errorMessage);
int main(int argc, char *argv[])
{
string request;
string response;
int resp_leng;
char buffer[BUFFERSIZE];
struct sockaddr_in serveraddr;
int sock;
WSADATA wsaData;
char *ipaddress = "210.125.167.240";
int port = 80;
//http://cwc.ghc.ac.kr/skin/base/board/view_brit2.gif
request+="GET /skin/base/board/view_brit2.gif HTTP/1.0rn";
request+="Host: cwc.ghc.ac.krrn";
request+="rn";
//init winsock
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
die_with_wserror("WSAStartup() failed");
//open socket
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
die_with_wserror("socket() failed");
//connect
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr(ipaddress);
serveraddr.sin_port = htons((unsigned short) port);
if (connect(sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
die_with_wserror("connect() failed");
//send request
if (send(sock, request.c_str(), request.length(), 0) != request.length())
die_with_wserror("send() sent a different number of bytes than expected");
//get response
response = "";
resp_leng= BUFFERSIZE;
while (resp_leng == BUFFERSIZE)
{
resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
if (resp_leng>0)
response+= string(buffer).substr(0,resp_leng);
}
ofstream myfile;
myfile.open ("C:\a\example.gif");
//display response
cout << response << endl;
//get response
response = "";
resp_leng= BUFFERSIZE;
while (resp_leng == BUFFERSIZE)
{
resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
if (resp_leng>0)
response+= string(buffer).substr(0,resp_leng);
}
//display response
cout << response << endl;
myfile << response;
myfile.close();
//disconnect
closesocket(sock);
//cleanup
WSACleanup();
return 0;
}
void die_with_error(char *errorMessage)
{
cerr << errorMessage << endl;
exit(1);
}
void die_with_wserror(char *errorMessage)
{
cerr << errorMessage << ": " << WSAGetLastError() << endl;
exit(1);
}
/*
It compiles and runs fine the output is:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
ETag: W/"60-1144808550000"
Last-Modified: Wed, 12 Apr 2006 02:22:30 GMT
Content-Type: image/gif
Content-Length: 60
Date: Wed, 22 Feb 2012 04:29:04 GMT
Connection: close
GIF89a
*/
怎么了,为什么不下载完整的图片?
看起来这个循环就是问题所在:
while (resp_leng == BUFFERSIZE)
{
resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
if (resp_leng>0)
response+= string(buffer).substr(0,resp_leng);
}
调用recv()
将从套接字中获得一些字节数,可能小于您所要求的。因此,与从文件中读取不同,请求BUFFERSIZE
字节可能会返回1到BUFFERSIZE
字节之间的任何位置。
相反,循环直到到达套接字的末尾:
while (true)
{
resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
if (resp_leng <= 0) {
break;
}
response+= string(buffer,resp_leng);
}
请注意,我还在循环的最后一行更改了对string()
构造函数的调用(您的调用会起作用,但会做更多的工作)。
您还应该在每个循环中清除接收缓冲区。将此作为循环中的第一行。
ZeroMemory(缓冲区,sizeof(缓冲区));
在缓冲区中保留二进制数据时,可能会得到很多意外的结果。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 最小硬币更换问题(自上而下方法)
- Qt VTK交互风格的信号到小部件
- 部分定义/别名模板模板参数
- 芬威克树(BIT).找到具有给定累积频率的最小索引,单位为 O(logN)
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- 使用递归的数组的最小值.这是怎么回事
- 为什么在浮点中从大到小会引入更多的误差
- 由于PC显示设置的更改,Qt小部件被部分剪裁
- 如何在编译时专门化大型模板函数中的小部分
- 将阵列分成更小的连续部分,以使 NEO 值最大化
- 输入将为浮动价值,程序返回整个部分和小数值
- 使用libtorrent下载特定的部分
- QT 事件仅对部分小部件透明
- 如何检测服务器允许多部分下载QT
- 把一个单词分成更小的部分
- 将部分字符*替换为较小的字符*
- 使用http get下载图片只会下载其中的一小部分
- 以小的部分读取大的二进制文件