HttpClient Get images from response

Ouerghi Yassine

I'm using Apache HttpClient to perform GET/POST requests,

I was wondering if you could save the images loaded/retrieved by a response, without having to download them again with their URLs.

This question has been asked like one year ago, but no one answered: Can I get cached images using HttpClient?

I tried:

CloseableHttpClient httpclient = HttpClients.createDefault();

HttpGet httpget = new HttpGet(url);

HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();

InputStream is = entity.getContent();

FileOutputStream fos = new FileOutputStream(new File("img.png"));
int inByte;
while ((inByte = is.read()) != -1) {
    fos.write(inByte);
}
is.close();
fos.close();

but apparently it's downloading only text, can i make HttpClient download images of that particular URL or not? Is this doable or not?

RealSkeptic

A web page is just the HTML code of the page.

When a browser accesses a webpage, it downloads the HTML code, and then parses the HTML. If there are things like IMG tags, embeded objects (like Flash, Applets etc.), frames and so on, the browser takes their URL, and creates a new HTTP connection, in which it downloads the image. It does so for every image. And then, having all the various parts of the page ready (in cache), it renders the page.

This is a simplified description, of course, as browsers tend to optimize these things by keeping connections open and keeping caches around. So to reiterate, to get the images in a page:

  1. Download HTML from the given URL.
  2. Parse the HTML and find the IMG tags.
  3. For every relevant IMG, download the image data from the SRC URL associated with it. You should save them to a file.

It is important to understand that an HttpClient response only represents one object - the HTML page, or a single image, depending what URL you gave it. If you want to download an entire page and all its images, you have to use an HttpClient for each of the objects yourself - it doesn't do so automatically.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

HttpClient Get images from response

From Dev

HttpClient response not get refresh

From Java

Angular 4.3.3 HttpClient : How get value from the header of a response?

From Dev

C# Get specific object from JSON response using HttpClient

From Dev

C# Get specific object from JSON response using HttpClient

From Dev

Phonegap app - How to get images from Json Web Service Response?

From Dev

Image caching from a HttpClient response

From Dev

How to get response body from httpclient c# when doing multipart

From Dev

Cannot get 'location' header in response using HttpClient

From Java

Decompressing GZip Stream from HTTPClient Response

From Dev

getting the whole response from HttpClient PostMethod

From Dev

Reading the response from HttpClient.GetStringAsync

From Dev

Response from System.Net.HttpClient

From Dev

How to get object using Httpclient with response Ok in Web Api

From Dev

Inject HttpClient to get mock response in Java using GUICE

From Dev

Get the Http response status using 'httpClient(postRequest,Handler)' method

From Dev

Anuglar5 HttpClient Get - how to convert response into a number

From Dev

Angular 5 Universal - Server side httpclient get response encode broken

From Dev

Differentiate time exceeded from no server response using HttpClient

From Dev

RequestEntityTooLarge response from Web Api when trying to upload a file with HttpClient

From Dev

The HttpClient fails to stop streaming after receiving a response from the server

From Dev

C# Flurl and HttpClient, no response from REST API

From Dev

HttpClient GetAsync response content is different from what Fiddler is giving me

From Dev

Angular5: Return HttpClient response from a service to a Component?

From Dev

How to format data from api response Angular HttpClient

From Dev

Angular 6 HttpClient Issue with mapping the response from API call

From Java

How to get content body from a httpclient call?

From Dev

How can I get cookie from httpclient?

From Dev

How to get the "Title" from a webpage using HttpClient

Related Related

  1. 1

    HttpClient Get images from response

  2. 2

    HttpClient response not get refresh

  3. 3

    Angular 4.3.3 HttpClient : How get value from the header of a response?

  4. 4

    C# Get specific object from JSON response using HttpClient

  5. 5

    C# Get specific object from JSON response using HttpClient

  6. 6

    Phonegap app - How to get images from Json Web Service Response?

  7. 7

    Image caching from a HttpClient response

  8. 8

    How to get response body from httpclient c# when doing multipart

  9. 9

    Cannot get 'location' header in response using HttpClient

  10. 10

    Decompressing GZip Stream from HTTPClient Response

  11. 11

    getting the whole response from HttpClient PostMethod

  12. 12

    Reading the response from HttpClient.GetStringAsync

  13. 13

    Response from System.Net.HttpClient

  14. 14

    How to get object using Httpclient with response Ok in Web Api

  15. 15

    Inject HttpClient to get mock response in Java using GUICE

  16. 16

    Get the Http response status using 'httpClient(postRequest,Handler)' method

  17. 17

    Anuglar5 HttpClient Get - how to convert response into a number

  18. 18

    Angular 5 Universal - Server side httpclient get response encode broken

  19. 19

    Differentiate time exceeded from no server response using HttpClient

  20. 20

    RequestEntityTooLarge response from Web Api when trying to upload a file with HttpClient

  21. 21

    The HttpClient fails to stop streaming after receiving a response from the server

  22. 22

    C# Flurl and HttpClient, no response from REST API

  23. 23

    HttpClient GetAsync response content is different from what Fiddler is giving me

  24. 24

    Angular5: Return HttpClient response from a service to a Component?

  25. 25

    How to format data from api response Angular HttpClient

  26. 26

    Angular 6 HttpClient Issue with mapping the response from API call

  27. 27

    How to get content body from a httpclient call?

  28. 28

    How can I get cookie from httpclient?

  29. 29

    How to get the "Title" from a webpage using HttpClient

HotTag

Archive