Handling missing resources with Apache HTTP in Java - 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.

Related

post encrypted json to external api in java

excuse me everyone, I'm currently having problems connecting my spring boot application with an open api IoT. to send data to the open api, the request body must be encrypted and sent in json form. I have followed the available documentation, but the response says that the request parameter must be in json format.
here is my program code:
HttpPost httppost = new HttpPost(url);
httppost.addHeader("x-random-key", randomSecretKey);
httppost.addHeader("x-access-key", xAccessKey);
httppost.addHeader("Content-type", "application/json;charset=UTF-8");
httppost.addHeader("sys_code", "901");
httppost.addHeader("lang", "_en_US");
String encryptedRequestBody = encrypt(request.toString(), randomMessage);
StringEntity strEntity = new StringEntity(encryptedRequestBody);
strEntity.setContentType("application/json");
httppost.setEntity(strEntity);
CloseableHttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();

Apache HttpPost Multiple Files upload failes with server error - Missing initial multi part boundary

I try to upload multiple files, using Apache Http library.
compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.5.6'
This is how I upload files.
String url = "url";
File f1 = new File("file1");
File f2 = new File("file2");
HttpPost request = new HttpPost(url);
request.addHeader("Content-Type", "multipart/form-data");
request.addHeader("Authorization", "Basic abcd=");
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody fileBody1 = new FileBody(f1, ContentType.DEFAULT_TEXT);
FileBody fileBody2 = new FileBody(f2, ContentType.DEFAULT_TEXT);
multipartEntityBuilder.addPart("file_1_param", fileBody1);
multipartEntityBuilder.addPart("file_2_param", fileBody2);
HttpEntity httpEntity = multipartEntityBuilder.build();
request.setEntity(httpEntity);
HttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
if (entity == null) {
return;
}
InputStream is = entity.getContent();
String textResponse = InputStreamUtils.readText(is);
System.out.println(textResponse);
It prints.
<pre> Server Error</pre></p><h3>Caused by:</h3><pre>java.lang.RuntimeException: javax.servlet.ServletException: org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io.IOException: Missing initial multi part boundary
at com.ca.devtest.acl.servlet.filters.RemoteAuthenticationFilter.doFilter(RemoteAuthenticationFilter.java:285)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1676)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
It works if I upload only one file though!
Note, this is not a duplicate. These links show posts which refer the problem on the server side. This problem is with the client side.
Jetty throws "Missing content for multipart request" on multipart form request
500 Internal server error Android HttpPost file upload
I found out a resolution. I wanted to share it with other people, so you can 1) learn how to upload files and 2) pay attention to HTTP headers.
When I append FileBody to MultipartEntityBuilder it automatically sets boundary. I simply removed this line from the code
request.addHeader("Content-Type", "multipart/form-data"); //Remove it
Example of a POST request with one or more files attached
The Multipart Content-Type
Remove the content-type option from the headers completely to make it work.
Example
headers = {
'cache-control': 'no-cache',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW',
'postman-token': '26ef68f6-e579-a029-aec2-09b291678b4d',
'storeid': '4223556'
}

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.

Why am I getting last response if my server doesn't work?

I use simple code to get XML content.
but I have a trouble if my server doesn't work, I get last success response.
I tried all methods:
send every time another URI
setHeader Cache-Content How to prevent Android from returning a cached response to my HTTP Request?
I tried even HttpURLConnection with GET.
but nothing helps
DefaultHttpClient client = new DefaultHttpClient();
String fullPath = path + name;
HttpGet request = new HttpGet(fullPath);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
//....decode input string

Is it possible to download files like PDF with HttpClient?

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.

Categories

Resources