I use the following code and run the method multiple times, a few times I get a response in GZIP which is what I expect and a few other times I get a response that is completely different(non GZIP page not found). However if I download the same URL multiple times using Mozilla or IE I consistently get the same GZIP response.
Is this an error with the server I am trying to reach to, or do I need to set any parameters to get a consistent response ?
The URL I am trying to download is the following, can you please let me know ?
public static byte[] dowloadURL(URL urlToDownload) {
InputStream iStream = null;
byte[] urlBytes = null;
try {
//HttpClient httpClient = new HttpClient();
org.apache.http.client.
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(urlToDownload.toString());
HttpResponse response = httpClient.execute(httpget);
iStream = response.getEntity().getContent();
urlBytes = IOUtils.toByteArray(iStream);
String responseString = new String(urlBytes);
System.out.println(" >>> The response string for " +urlToDownload.toString()+ " is " +responseString);
} catch (IOException e) {
System.err.printf("Failed while reading bytes from %s: %s",
urlToDownload.toExternalForm(), e.getMessage());
e.printStackTrace();
// Perform any other exception handling that's appropriate.
} finally {
if (iStream != null) {
try {
iStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return urlBytes;
}
Related
I have the following code using try with resources with CloseableHttpResponse
CloseableHttpResponse response = null;
try (CloseableHttpClient httpClient = HttpClients.custom().build()){
//code...
response = httpClient.execute(target, post);
String responseText = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
logger.error("Failed sending request", e);
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
logger.error("Failed releasing response", e);
}
}
}
Can I safely replace with nested try with resources:
try (CloseableHttpClient httpClient = HttpClients.custom().build()){
URIBuilder uriBuilder = new URIBuilder(url);
HttpHost target = new HttpHost(uriBuilder.getHost(), uriBuilder.getPort(), uriBuilder.getScheme());
HttpPost post = new HttpPost(uriBuilder.build());
try (CloseableHttpResponse response = httpClient.execute(target, post)) {
String responseText = EntityUtils.toString(response.getEntity());
}
} catch (Exception e) {
logger.error("Failed sending request", e);
}
Or is it better to use a single try with resources block:
try (CloseableHttpClient httpClient = HttpClients.custom().build();
CloseableHttpResponse response = getResponse(httpClient, url)) {
Sometime refactoring to single block is problematic, so I wanted to know the a nested/additional block is a valid solution.
HttpClient never returns a null HttpResponse object. The first construct is simply not useful. Both the second and the third constructs are perfectly valid
I am getting an error with the following line of code
HttpClient Client= new HttpClient, event I have add httpclient-4.2.3.jar to my project.
I have tried rewriting it as HttpClient Client= new DefaultHttpClient. But it solves the problem and creates a new error with other methods (in executeMethod()).
This is my code :
public static String POSTUpload(String url, String foto){
InputStream inputStream=null;
String result="";
HttpClient httpClient = new HttpClient();
PostMethod method = new PostMethod(url);
//set JSON ke hhtp post entity
// File file_foto= new File(foto);
// FileEntity fe_foto=new FileEntity(file_foto, "image/jpeg");
method.setRequestEntity(new FileRequestEntity(new File(foto), "multipart/form-data"));
// se.setContentType("application/json;charset=UTF-8");
// httpPost.setHeader("Accept", "application/json");
try {
int status = httpClient.executeMethod(method);
System.out.println("HTTP status " + method.getStatusCode()
+ " creating con\n\n");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); // TODO Auto-generated catch block
}finally {
method.releaseConnection();
}
}
is there anyone know why this is happen? Please Help
I have the code which basically retrieving a list of search result based on given keyword from TMDB (API ver.3, new API).
public String getPersonSearchResult(String keywords){
String query = URLEncoder.encode(keywords);
String TMDB_API_URL = "http://api.themoviedb.org/3/search/person?";
String TMDB_LIMIT_LIST = "&page=1";
String TMDB_QUERY = "&query=" + query;
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try
{
// ATTEMPT HTTP REQUEST
String fullUrl = TMDB_API_URL + TMDB_API_KEY + TMDB_QUERY + TMDB_LIMIT_LIST;
Log.w(APP_TAG, "TRYING [" + fullUrl + "]");
response = httpclient.execute(new HttpGet(fullUrl));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK)
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
}else{
// FAILED REQUEST - CLOSE THE CONNECTION
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
}catch(Exception e){
Log.w(APP_TAG, e.getLocalizedMessage());
Log.w(APP_TAG, "FAILED TO RETRIEVE JSON DATA");
}
return responseString;
}
The problem is that i always get 406 Status Code (Not Acceptable). When i tried to run the URL myself
http://api.themoviedb.org/3/search/person?api_key=<MY_API_KEY_HERE>&query=jennifer&page=1
It displays the JSON result correctly.
I am not sure why is this happening. Similar function is used to retrieve JSON value from other source and and it works perfectly.
this is their API docs regarding search: http://docs.themoviedb.apiary.io/#search
Can anyone points me to the right direction? Any help is appreciated.
I figure it out, by adding this:
HttpGet getObj = new HttpGet(fullUrl);
getObj.addHeader("Accept", "application/json");
Perhaps this is API specific requirement. Not sure though...
One way to do it
Try using builder.scheme
URIBuilder builder = new URIBuilder();
builder.setScheme("http").setHost("api.themoviedb.org").setPath("/3/search/person")
.setParameter("api_key", YOURAPIKEY)
.setParameter("page", 1)
.setParameter("query", query)
URI uri = builder.build();
HttpGet httpget = new HttpGet(uri);
.....
response = httpclient.execute(new HttpGet(httpget ));
I think its nothing to do with your code.. your code is perfect. The only problem might be with the API request method. Maybe they require some specific headers to be requested for in the request. Give a try with requesting headers like "("Accept", "application/json")" It might work..
try this
String ret = null; try {
response = httpClient.execute(httpGet);
} catch (Exception e) {
e.printStackTrace();
}
try {
ret = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
Log.e("HttpRequest", "" + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
return ret;
I've never really used http requests in Java, I'm trying to make a request that would basically recreate this http://supersecretserver.net:8080/http://whateverwebsite.com
This server takes whatever website and returns only the text of the page in the body of the response.
The code is as follows:
public String getText(String webPage) throws ParseException, IOException{
HttpResponse response = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://supersecretserver.net:8080/" + "http://www.androidhive.info/2012/01/android-text-to-speech-tutorial/"));
response = client.execute(request);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String responseBody = "No text found on webpage.";
int responseCode = response.getStatusLine().getStatusCode();
switch(responseCode) {
case 200:
HttpEntity entity = response.getEntity();
if(entity != null) {
responseBody = EntityUtils.toString(entity);
}
}
System.out.println("Returning Response..");
System.out.println(responseBody);
return responseBody;
}
It seems to get stuck on
response = client.execute(request);
I'm not sure what the problems is, any insight would be helpful.
Seems likely that your HttpClient is not timing out, you can set a timeout value by following this example (from http://www.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/)
You just to have to consider a timeout value that makes sense for you.
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeoutMillis);
HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutMillis);
Also as your HttpClient is not connecting (since it's getting stuck) you should also take into consideration why is that happening (maybe you need to configure a proxy?)
public class CacheDemo {
public static void main(String[] args) {
CacheConfig cacheConfig = new CacheConfig();
cacheConfig.setMaxCacheEntries(1000);
cacheConfig.setMaxObjectSizeBytes(1024 * 1024);
HttpClient cachingClient = new CachingHttpClient(new DefaultHttpClient(), cacheConfig);
HttpContext localContext = new BasicHttpContext();
sendRequest(cachingClient, localContext);
CacheResponseStatus responseStatus = (CacheResponseStatus) localContext.getAttribute(
CachingHttpClient.CACHE_RESPONSE_STATUS);
checkResponse(responseStatus);
sendRequest(cachingClient, localContext);
responseStatus = (CacheResponseStatus) localContext.getAttribute(
CachingHttpClient.CACHE_RESPONSE_STATUS);
checkResponse(responseStatus);
}
static void sendRequest(HttpClient cachingClient, HttpContext localContext) {
HttpGet httpget = new HttpGet("http://www.mydomain.com/content/");
HttpResponse response = null;
try {
response = cachingClient.execute(httpget, localContext);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpEntity entity = response.getEntity();
try {
EntityUtils.consume(entity);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void checkResponse(CacheResponseStatus responseStatus) {
switch (responseStatus) {
case CACHE_HIT:
System.out.println("A response was generated from the cache with no requests "
+ "sent upstream");
break;
case CACHE_MODULE_RESPONSE:
System.out.println("The response was generated directly by the caching module");
break;
case CACHE_MISS:
System.out.println("The response came from an upstream server");
break;
case VALIDATED:
System.out.println("The response was generated from the cache after validating "
+ "the entry with the origin server");
break;
}
}
}
It is not worked for me.
Every time it get the data from the server.not from the cache.
I m using jar "httpclient-cache-4.1-beta1".
You haven't showed us what's going on with your HTTP server. Is the service at mydomain.com/content setting the correct Cache-Control headers on the HTTP response? For caching to work, you need to have your HTTP server or web application indicate if the data can be cached and the length that the data can be cached using the appropriate headers.
Also, check the API documented on CachingHttpClient to see what headers it expects from the web server.