connection persistence using httpclient - java

i do multiple request to the same url using httpclient.execute(request).
Can I re-use the connection for the consecutive requests?
how can i optimise the code without declaring HttpClient again and again.
for(int i=0;i<=50;i++)
{
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("my_url");
HttpResponse response = client.execute(request);
System.out.println(response.getStatusLine().getStatusCode());
}

In order to use a single client in your code (based on Exception using HttpRequest.execute(): Invalid use of SingleClientConnManager: connection still allocated and Lars Vogel Apache HttpClient - Tutorial):
Step 1. Move the client generation outside the for-loop.
Step 2. You should read the response content and close the stream. If you don't do this you will get the following exception
Exception in thread "main" java.lang.IllegalStateException:
Invalid use of SingleClientConnManager: connection still allocated.
In code:
//step 1
HttpClient client = new DefaultHttpClient();
for(int i=0;i<=50;i++) {
HttpGet request = new HttpGet("my_url");
HttpResponse response = client.execute(request);
System.out.println(response.getStatusLine().getStatusCode());
//step 2
BufferedReader br = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
//since you won't use the response content, just close the stream
br.close();
}

try below.
HttpUriRequest httpGet = new HttpGet(uri);
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
HttpResponse httpResponse = defaultHttpClient.execute(httpGet);

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.

Apache HttpClient Persistent Connection usage

What is the right way for me to use the same TCP connection when using Apache HttpClient?
My code currently is:
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpClientContext httpContext = HttpClientContext.create();
for (int i = 0; i < 100; i++)
{
CloseableHttpResponse response = httpClient.execute(new HttpGet("http://www.google.co.uk"), httpContext);
String responseBody = EntityUtils.toString(response.getEntity());
EntityUtils.consume(response.getEntity());
response.close();
}
I have tried using the code with and without response.close() but the times vary each run that I can't figure out which one is keeping the connection open.
Can somebody please explain to me how I can keep the connection open?
So after messing around with TCPView I figured out that placing the lines:
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpClientContext httpContext = HttpClientContext.create();
inside of the loop used a new TCP connection each time. Turns out that HttpClient will automatically try and reuse the connection for the same 'HttpClient' object.

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

Setting a timeout value when retrieving data via HttpGet object

I have some Java code that I 'inherited' from a previous co-worker. Part of it connects to an outside URL using method GET and retrieves a small amount of XML for parsing. We've been having issues recently with this connection crashing our website due to the vendor website hanging and using up resources on our side. One issue is due to no timeouts being set when our code uses the HttpGet object. Is there a way to fine-tune timeouts using this object, or is there a better way to pull back this XML?
Would I be better off using another API?
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("param1","foobar"));
URI uri = URIUtils.createURI("http", "myhost.com", -1, "mypath",
URLEncodedUtils.format(params, "UTF-8"), null);
// there is no timeout here??
HttpGet httpGet = new HttpGet(uri);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpGet);
String result = IOUtils.toString(httpResponse.getEntity()
.getContent(), "UTF-8");
Thanks!
try this instead
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("param1","foobar"));
URI uri = URIUtils.createURI("http", "myhost.com", -1, "mypath",
URLEncodedUtils.format(params, "UTF-8"), null);
HttpGet httpGet = new HttpGet(uri);
HttpClient httpClient = new DefaultHttpClient();
// set the connection timeout value to 30 seconds (30000 milliseconds)
final HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
httpClient = new DefaultHttpClient(httpParams);
HttpResponse httpResponse = httpClient.execute(httpGet);
String result = IOUtils.toString(httpResponse.getEntity().getContent(), "UTF-8");
From Java HTTP Client Request with defined timeout

Multiple GET requests to a single site (canonical) (java)

Hi I am trying to make 2 GET requests to a single connection. ie
HttpGet get1 = new HttpGet("http://www.google.com/search?q=HelloWorld");
HttpGet get2 = new HttpGet("http://www.google.com/search?q=SecondSearch");
HttpResponse response = null;
response = client.execute(get1);
response = client.execute(get2);
I would like to get the body from the second execution. Obviously this fails, because it says you must release the connection first. I need to maintain the exact session - for instance, if I navigate to a site where the first step is to login, I need to navigate to any subsequent pages with the same cookie.
It's probably something incredibly simple that I am doing wrong!
You need to use a CookieStore
CookieStore cookieStore = new BasicCookieStore();
DefaultHttpClient client1 = new DefaultHttpClient();
client1.setCookieStore(cookieStore);
HttpGet httpGet1 = new HttpGet("...");
HttpResponse response1 = client1.execute(httpGet1);
DefaultHttpClient client2 = new DefaultHttpClient();
client2.setCookieStore(cookieStore);
HttpGet httpGet2 = new HttpGet("...");
HttpResponse response2 = client2.execute(httpGet2);
In the above code, both client2 will re-use cookies from the client1 request.

Categories

Resources