Is it possible to download files like PDF with HttpClient? - java

I found some examples here on how to download a file but most of them seem to be using HttpURLConnection. is it possible to download files with HttpClient?

Using httpclient is pretty easy. Here's a link to it's tutorial.
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e43
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(urltofetch);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
long len = entity.getContentLength();
InputStream inputStream = entity.getContent();
// write the file to whether you want it.
}

Anything you can do with HttpURLConnection you can do, usually better, with HttpClient look through their examples about file transfer and you will see how.

Related

How to Get Cached HttpResponse (Apache HttpClient)

I need to compare the results of my old (cached) response and the new response I got from a certain request. But I have no idea how to get the cached response.
CloseableHttpClient httpClient = CachingHttpClients.createMemoryBound();
CloseableHttpResponse httpResponse = httpClient(new HttpGet("http://www.example.com/path/to/file.json"));
InputStream fromUpstream = response.getEntity().getContent();
InputStream fromCache = ???;
// Compare fromUpstream and fromCache
...
What I’ve been doing up until now is use an HttpCacheStorage to do this, like so:
HttpCacheStorage cacheStorage = new BasicHttpCacheStorage(CacheConfig.DEFAULT);
CloseableHttpClient httpClient = CachingHttpClients.custom()
.setHttpCacheStorage(cacheStorage)
.build();
String url = "http://www.example.com/path/to/file.json";
CloseableHttpResponse httpResponse = httpClient(new HttpGet(url));
InputStream fromUpstream = httpResponse.getEntity().getContent();
InputStream fromCache = cacheStorage.getEntry(constructCacheEntryKeyFromUrl(url)).getResource().getInputStream();
And this works. But what I hate about it is the fact that the key for the cached entry is not-so-straightforward. I have to reconstruct the URL to include a port number (i.e. http://www.example.com:80/path/to/file.json).
I know that technically, I'm comparing InputStreams, but it'd be great if I can compare actual HttpResponses.

Jakarta HttpClient issue with size of tag

I have service witch is returning rather simple XML in body, but with very large text in "ns:responses" tag - in IE/Chrome it is approx. 40kB, but in HttpClient I am getting:
<ns:InvokeResponse xmlns:ns="...">
<ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns:ServiceResponse">
<ns:responses>TOO MANY CHARACTERS</ns:responses>
<ns:returnCode>0</ns:returnCode>
</ns:return>
</ns:InvokeResponse>
I am using code from example of HttpClient:
CloseableHttpClient httpclient = HttpClients.createMinimal();
try {
HttpGet httpGet = new HttpGet(request);
CloseableHttpResponse response1 = httpclient.execute(httpGet);
try {
System.out.println(response1.getStatusLine());
HttpEntity entity1 = response1.getEntity();
entity1.writeTo(new FileOutputStream("out.txt"));
EntityUtils.consume(entity1);
} finally {
response1.close();
}
} finally {
httpclient.close();
}
Any idea what might how can it be improved that the library will correctly read big chunk of characters in one tag so I have correct data instead of 'TOO MANY CHARACTERS'?
The application/library is fine. This was error in URL and the answer was correct answer from server.

Handling missing resources with Apache HTTP in Java

I'm using HttpEntity from Apache library to download files from URLs. I.e.
String url="http://www.stackoverflow.com/question/ask/idontexist.jpg";
String user_agent=...; //I know, I can use the default value, but this is what I do actually!
HttpClient httpclient =new AutoRetryHttpClient(new DefaultServiceUnavailableRetryStrategy(5, 500));
HttpGet httpget = new HttpGet(url);
httpget.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpget.setHeader("User-Agent", user_agent);
HttpEntity entity = httpclient.execute(httpget).getEntity();
InputStream is = entity.getContent();
Now. If I save the resource from the InputStream through a FileOutputStream I get a file named idontexist.jpg, but it has no content (as expected).
How can I verify that the returned InputStream has no content or that the requested resource pointed by the URL doesn't exist?
You should first get HttpResponse object with
HttpResponse httpResponse = httpclient.execute(httpget);
Then you can get status code with
int statusCode = httpResponse.getStatusLine().getStatusCode();
and, if the resource is found, get http entity with
HttpEntity entity = httpResponse.getEntity();
Hope this helps,
Regards.

How to use HttpClient

I'm currently trying to get some data via a 'uri' using the following code in java:
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(uri);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
if(entity != null){
InputStream stream = entity.getContent();
callString = stream.toString();
return callString;
}
However this isn't working. Does anyone know what I'm doing wrong here?
You cannot print out the input stream like that... instead, do something like this:-
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://ichart.finance.yahoo.com/table.csv?s=MSFT");
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
if (entity != null) {
Scanner scanner = new Scanner(entity.getContent());
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
}
The printout looks like this:-
1994-02-02,84.75,85.50,84.00,84.00,40924800,2.09
1994-02-01,85.00,85.75,84.50,85.12,44003200,2.12
1994-01-31,85.25,85.87,84.75,85.12,62566400,2.12
1994-01-28,84.50,85.50,84.25,84.87,41875200,2.11
1994-01-27,84.00,84.75,83.25,84.25,51129600,2.10
1994-01-26,85.00,85.00,84.00,84.25,50489600,2.10
1994-01-25,85.25,85.37,84.00,85.12,70361600,2.12
...
Its a total guess but shouldn't it be:
String uri = "ichart.finance.yahoo.com/table.csv?s=MSFT"
HttpData data = HttpRequest.get(uri);
System.out.println(data.content);
you are trying to download a file and getEntity is used get an object for a type you have specified. IMHO this wont work.
You need to code which will actually read the response stream and read contents out of it...
What are you trying to do ?
To read the resulting entity to a String use EntityUtils.toString(HttpEntity) or EntityUtils.toString(HttpEntity, String) if you know the character set.
If you are getting NetworkOnMainThreadException then that means you are calling client.execute(get); on the main thread which is an exception thrown on Honeycomb and higher. See http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html for details. The solution is to run this in a new thread.

InputStreamBody in HttpMime 4.0.3 settings for content-length

I am trying to send a multi part formdata post through my java code. Can someone tell me how to set Content Length in the following?? There seem to be headers involved when we use InputStreamBody which implements the ContentDescriptor interface. Doing a getContentLength on the InputStreamBody gives me -1 after i add the content. I subclassed it to give contentLength the length of my byte array but am not sure if other headers required by ContentDescriptor will be set for a proper POST.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(myURL);
ContentBody cb = new InputStreamBody(new ByteArrayInputStream(bytearray), myMimeType, filename);
//ContentBody cb = new ByteArrayBody(bytearray, myMimeType, filename);
MultipartEntity mpentity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
mpentity.addPart("key", new StringBody("SOME_KEY"));
mpentity.addPart("output", new StringBody("SOME_NAME"));
mpentity.addPart("content", cb);
httpPost.setEntity(mpentity);
HttpResponse response = httpclient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
I'm the author of the ByteArrayBody class you have commented out.
I wrote it because I faced the same issue you did. The original Jira ticket is here: https://issues.apache.org/jira/browse/HTTPCLIENT-1014
So, since you already have a byte[], either upgrade HttpMime to the latest version, 4.1-beta1, which includes this class. Or copy the code from the Jira issue into your own project.
The ByteArrayBody class will do exactly what you need.

Categories

Resources