我正在尝试从http://htmlbook.ru/themes/hb/img/logo.png
下载图像
这样,我下载了zip归档文件,它没有任何问题地打开,仅将扩展名更改为.zip。
我的代码
const char* url="www.htmlbook.ru";
const char*path="/themes/hb/img/logo.png";
FILE* file;
file = fopen("down.png", "wb");
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NULL) std::cout << "Error";
SOCKET req;
char buf[1024];
hostent*hn;
char request[2056];
sockaddr_in adr;
if (INVALID_SOCKET == (req = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))) std::cout << "Error 1";
if (NULL == (hn = gethostbyname(url))) std::cout << "Error 2";
adr.sin_family = AF_INET;
adr.sin_addr.S_un.S_addr = *(DWORD*)hn->h_addr_list[0];
adr.sin_port = htons(80);
strcpy(request, "GET ");
if (path == NULL) {
strcat(request, "/");
}
else strcat(request, path);
strcat(request, " HTTP/1.1\r\nHost: ");
strcat(request, url);
strcat(request, "\r\nAccept: */*\r\nAccept: image / png\r\n\r\n");
printf("%s", request);
if (SOCKET_ERROR == connect(req, (sockaddr*)&adr, sizeof(adr))) std::cout << "Error 3\n";
if (SOCKET_ERROR == send(req, request, sizeof(request), 0)) std::cout << "Error 4\n";
int len;
do {
if (SOCKET_ERROR == (len = recv(req, (char *)&buf, sizeof(buf), 0))) { std::cout << "Error 5\n"; break; }
for (int i = 0; i < len; i++) {
fputc(buf[i], file);
}
} while (len != 0);
if (SOCKET_ERROR == closesocket(req)) std::cout << "Error 6\n";
为什么服务器两次回应我? 响应负责人
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 02 Nov 2018 18:45:50 GMT
Content-Type: image/png
Content-Length: 3357
Last-Modified: Sun, 28 Jul 2013 05:13:46 GMT
Connection: keep-alive
Keep-Alive: timeout=20
ETag: "51f4a88a-d1d"
Expires: Fri, 16 Nov 2018 18:45:50 GMT
Cache-Control: max-age=1209600
Accept-Ranges: bytes
HTTP/1.1 400 Bad Request
Server: nginx
Date: Fri, 02 Nov 2018 18:45:50 GMT
Content-Type: text/html
Content-Length: 166
Connection: close
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
我正在记事本中打开图像,并删除了二进制文本之外的服务器响应,但是我无法在查看照片窗口中打开图像 如何下载没有响应头的文件。