如何使用基于TCP的HTTP下载图像? [英] How to use TCP-based HTTP to download image?
问题描述
我得到了一些图像使用HTTP下载。我得到了这些图片'URL,如何构建基于TCP的HTTP缓存下载图像?
I got some images to download using HTTP. I got these images' URL, how to build the TCP-based HTTP buffer to download the image?
我在我的当前平台没有图书馆,在这个平台上唯一支持的语言是C,所以我要建立对这些资源的HTTP缓存。
I got no library in my current platform, the only supported language in this platform is C, so I have to build the HTTP buffer for these resources.
目前我已经建立了正常的API请求,他们都是HTTP请求,每次请求有0个或多个参数。但图像请求只拿到了一个URL,如 HTTP://some-image.jpg ,这似乎只是一个下载工作,没有API的参数,没有授权,它的简单,但如何构建TCP请求?
Currently I have build the normal API request, they are all HTTP request, every request have 0 or more parameters. But the image request got only a URL, such as http://some-image.jpg, it seems just a download job, no API parameters, no authorization, it's simple, but how to construct the TCP request?
推荐答案
按照 HTTP: //www.jmarshall.com/easy/http/#sample ,我建立一个像我的TCP请求:
According to the http://www.jmarshall.com/easy/http/#sample, I build my TCP request like that:
sprintf(tcp_send_buf, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", img_path, img_host);
/* I wrapped TCP APIs for convenient, hope you understand it... */
set_host_and_port(img_host, 80);
tcp_send(tcp_send_buf, strlen(tcp_send_buf), recv_callback);
在我的 recv_callback
,我得到了这样的服务器响应:
On my recv_callback
, I got the server response like that:
HTTP/1.1 200 OK
Content-Length: 42299
Content-Type: image/jpeg
Last-Modified: Mon, 02 Jul 2007 07:58:47 GMT
Accept-Ranges: bytes
ETag: "e2c8b5d17ebcc71:15d5"
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Fri, 25 May 2012 01:33:57 GMT
<binary image data>
我下载从Chrome中的图像,它的大小是一样的的Content-Length:42299
,我觉得我得到的图像缓存
I downloaded the image from Chrome, and it's size is the same as Content-Length: 42299
, I think I got the image buffer.
这篇关于如何使用基于TCP的HTTP下载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!