3

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());
}
muchumanoj
  • 125
  • 1
  • 8
  • 1
    yes, i tried searching a lot. but i am not able to find a sample code – muchumanoj Feb 27 '13 at 06:35
  • yes, i tried a lot and lot of changes, but I am not able to. If you find it very silly for a developer to ask, then please help me out with it. I put the client outside and tried also, it gives the error: connection still open. how do i re use it. this would have been the first thing i tried. – muchumanoj Feb 27 '13 at 06:40

2 Answers2

8

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();
}
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • wow. it worked. i just closed the stream and it worked. i was thinking i never opened the stream, i was just reading the status code and no need to open the response and close it. Anyways thanks a lot:):) – muchumanoj Feb 27 '13 at 07:10
  • i get the error: org.apache.http.client.protocol.ResponseProcessCookies processCookies. can you help me with this. – muchumanoj Feb 27 '13 at 07:10
  • @user2071013 related: http://stackoverflow.com/q/7459279/1065197 check the first and second answers in order to solve the problem – Luiggi Mendoza Feb 27 '13 at 07:12
  • thank you. but the first answer says it cannot be fixed and the second answer did not work. i think it is the server issue and we cant handle it in our code. – muchumanoj Feb 27 '13 at 07:19
  • @user2071013 another related answer: http://stackoverflow.com/a/4595054/1065197 after this, I can't find another solution =\ – Luiggi Mendoza Feb 27 '13 at 07:20
0

try below.

HttpUriRequest httpGet = new HttpGet(uri);
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
HttpResponse httpResponse = defaultHttpClient.execute(httpGet);
Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
Jwalin Shah
  • 2,451
  • 1
  • 17
  • 22
  • i still get : Exception using HttpRequest.execute(): Invalid use of SingleClientConnManager: connection still allocated – muchumanoj Feb 27 '13 at 07:08
  • 2
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – msrd0 Mar 10 '15 at 16:00