使用http get下载图片只会下载其中的一小部分

Downloading a picture with http get only downloads a small part of it

本文关键字:下载 小部分 http get 使用      更新时间:2023-10-16

我正在尝试使用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(缓冲区));

在缓冲区中保留二进制数据时,可能会得到很多意外的结果。