I'm using HttpClient 4.3 with a PoolingHttpClientConnectionManager and two threads which each connect to a REST API. Both are permanently sending requests. It works well for some minutes, but then, the requests begin to timeout. While browsing, even Chrome cannot establish connections to websites. This is my code for sending a request:
String sendGETApache(HttpGet httpGet) {
try {
CloseableHttpResponse response = httpClient.execute(httpGet); //, httpContext);
HttpEntity entity = null;
try {
entity = response.getEntity();
if(entity != null) {
String s = EntityUtils.toString(entity);
return s;
}
} catch (Exception e) {
e.printStackTrace();
httpGet.abort();
} finally {
EntityUtils.consume(entity);
response.close();
}
} catch (Exception e) {
httpGet.abort();
e.printStackTrace();
} finally {
httpGet.releaseConnection();
}
return "";
}
The HttpClient is an unique object, shared between both threads.
I tried already limiting DefaultMaxPerRoute and MaxTotal to 1 and 2, I even used really big values, no result.
I even implemented an extra thread which calls closeExpiredConnections() and closeIdleConnections() repeatedly, also no improvement.
Do you have any idea what I could do? I think somewhere resources are leaking, but I don't knwo where...
Thank you
Related
I'm seeing something really weird. Im making a http post request
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String strResponse = null;
if (entity != null) {
strResponse = EntityUtils.toString(entity);
Log.i("SetupAutoCardResponse",String.valueOf(strResponse));
Log.i("otherTest","test");
Log.i("otherTest",strResponse);
return true;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
The only output I see in the log is otherTest : Test.
The logs with strResponse dont show up at all, and by that I mean, nothing shows. It does not look like this SetupAutoCardResponse : null or otherTest: null. Absolutly nothing shows up. But I know I am getting a response. The expression returns true
In Log, if the Messsage is Null or Blank or Empty, Log will not write down.
Example: Log.i(TAG, MSG);
To make sure it works, we can add some text to MSG
Log.i(TAG, String.format("%s %s", "MSG>" , MSG));
I am trying to authenticate to a Windows server running IIS that is configured for Windows Integrated Authentication (SPNEGO) using Apache HttpClient 4.3. My code looks very similar to that of the sample code I've been able to locate online, but when I run it I consistently get an HTTP 401 returned. I ran Wireshark on the results, and do not see the SPNEGO token being passed on to the server.
I'm able to hit the protected resource just fine via a web browser, and in this case I do see the SPNEGO token. The behavior is different when I run my code, though. Here is the code in question:
public static void main(String[] args) {
System.setProperty("java.security.krb5.conf",
"c:\\develop\\XYZ\\KerberosTest\\conf\\krb5.conf");
System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
System.setProperty("java.security.auth.login.config",
"c:\\develop\\XYZ\\KerberosTest\\conf\\login.conf");
Credentials jaasCredentials = new Credentials() {
public String getPassword() {
return null;
}
public Principal getUserPrincipal() {
return null;
}
};
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(null, -1, null),
jaasCredentials);
Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder
.<AuthSchemeProvider> create().register(AuthSchemes.SPNEGO,
new SPNegoSchemeFactory()).build();
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultAuthSchemeRegistry(authSchemeRegistry)
.setDefaultCredentialsProvider(credsProvider).build();
try {
HttpGet httpget = new HttpGet(ENDPOINT);
RequestLine requestLine = httpget.getRequestLine();
CloseableHttpResponse response = httpclient.execute(httpget);
try {
StatusLine status = response.getStatusLine();
HttpEntity entity = response.getEntity();
if (entity != null) {
}
EntityUtils.consume(entity);
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I believe I have configured my krb5.conf file correctly, and my login.conf file is taken directly from the Apache HttpClient documentation. I've also made the appropriate registry key change, as mentioned in the docs.
Any idea what could be causing this or how I could go about troubleshooting? Is there a step or line I am missing?
Problem solved. This appears to be due to a bug in IBM's JDK. Once I changed to Sun's, everything works.
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.
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;
}