Java Reference
In-Depth Information
int httpStatus = client.executeMethod(get);
if (HttpStatus.SC_OK == httpStatus) {
InputStream responseBody =
get.getResponseBodyAsStream();
System.out.println("Response Size=" +
get.getResponseContentLength());
System.out.println("Response Type=" +
get.getResponseHeader("Content-Type"));
//save retrieved file on local path
File outputFile = new File(SAVE_TO);
BufferedImage img = ImageIO.read(responseBody);
ImageIO.write(img, "gif", outputFile);
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//clean up
get.releaseConnection();
}
}
}
The methods in the DifferentRepClient class all follow the same basic pattern: they create
an HttpClient instance, create a class representing the HTTP GET method to send a request,
and then specify the HTTP Accept header on the outgoing request to match the representation
they want to get back. This is fairly straightforward for the HTML and XML types; the main
difference within the image getter method is that it gets the body as a stream, and then saves
the response out as an image to the current directory using the Java Image IO library.
NOTE
All of the invocations of the service point to the exact same URL—the single difference between
them is the use of the Accept header. There is no query parameter or any other mechanism to distin-
guish what method the service should dispatch to for generating a response.
The third class required in this application is just a little Swing image viewer helper class
used by the client to prove that it successfully retrieved the representation presented in GIF
Search WWH ::




Custom Search