How to use TCP-based HTTP to download image?

2019-02-21 03:08发布

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?

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.

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?

标签: c http tcp
3条回答
家丑人穷心不美
2楼-- · 2019-02-21 03:26

how to build the TCP-based HTTP buffer to download the image?

Stop thinking TCP. It has it's own buffers which have nothing to do with what's happenning at the HTTP level.

You really don't want to implement your own HTTP stack - it's not trivial. There are several well-written ones already available - I'd recommend using libcurl.

查看更多
唯我独甜
3楼-- · 2019-02-21 03:27

You would have to implement HTTP protocol or a subset of it. There are open source implementations. For example:

https://github.com/bagder/curl/tree/master/lib

https://github.com/joshthecoder/libhttp

查看更多
Anthone
4楼-- · 2019-02-21 03:30

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);

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>

I downloaded the image from Chrome, and it's size is the same as Content-Length: 42299, I think I got the image buffer.

查看更多
登录 后发表回答