Exception using HttpRequest.execute (): Invalid use of SingleClientConnManager: connection is still highlighted

I use google-api-client-java 1.2.1-alpha to execute a POST request, and when I execute () the HttpRequest, I get the following stack.

This happens immediately after I catch and ignore the 403 error from the previous POST to the same URL and reuse the transport for the subsequent request. (This is in a loop that inserts several records into the same ATOM channel).

Is there something I have to do to β€œclean” after 403?

Exception in thread "main" java.lang.IllegalStateException: Invalid use of SingleClientConnManager: connection still allocated. Make sure to release the connection before allocating another one. at org.apache.http.impl.conn.SingleClientConnManager.getConnection(SingleClientConnManager.java:199) at org.apache.http.impl.conn.SingleClientConnManager$1.getConnection(SingleClientConnManager.java:173) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:390) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554) at com.google.api.client.apache.ApacheHttpRequest.execute(ApacheHttpRequest.java:47) at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:207) at au.com.machaira.pss.gape.RedirectHandler.execute(RedirectHandler.java:38) at au.com.machaira.pss.gape.ss.model.records.TableEntry.executeModification(TableEntry.java:81) 

Why is the code below me trying to get a new connection?

+80
java google-api-java-client
Jan 06 2018-11-11T00:
source share
9 answers

You need to use the response body before you can reuse the connection for another request. You should not only read the status of the response, but also completely read the InputStream response to the last byte, in which you simply ignore the read bytes.

+80
Jan 07 2018-11-11T00:
source
β€” -

I ran into a similar problem when using HttpClient with Jetty to create a test environment. I had to create several Servelet requests from my client, but it was throwing the same exception on execution.

I found an alternative at http://foo.jasonhudgins.com/2010/03/http-connections-revisited.html

You can also use this following method to instantiate your client.

 public static DefaultHttpClient getThreadSafeClient() { DefaultHttpClient client = new DefaultHttpClient(); ClientConnectionManager mgr = client.getConnectionManager(); HttpParams params = client.getParams(); client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params); return client; } 
+42
Jul 18 2018-11-18T00:
source

A similar exception message (since at least Apache Jarkata Commons HTTP Client 4.2):

java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated. Make sure to release the connection before allocating another one.

This exception may occur if two or more threads interact with one org.apache.http.impl.client.DefaultHttpClient .

How can you create a streaming application 4.2 DefaultHttpClient (thread safe in the sense that two or more threads can interact with it without receiving an error message)? Provide the DefaultHttpClient connection ClientConnectionManager in the form org.apache.http.impl.conn.PoolingClientConnectionManager !

 /* using <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.2.2</version> </dependency> */ import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.params.HttpConnectionParams; import org.apache.http.client.HttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.PoolingClientConnectionManager; import org.apache.http.impl.conn.SchemeRegistryFactory; import org.apache.http.params.HttpParams; import org.apache.http.client.methods.HttpGet; public class MyComponent { private HttpClient client; { PoolingClientConnectionManager conMan = new PoolingClientConnectionManager( SchemeRegistryFactory.createDefault() ); conMan.setMaxTotal(200); conMan.setDefaultMaxPerRoute(200); client = new DefaultHttpClient(conMan); //The following parameter configurations are not //neccessary for this example, but they show how //to further tweak the HttpClient HttpParams params = client.getParams(); HttpConnectionParams.setConnectionTimeout(params, 20000); HttpConnectionParams.setSoTimeout(params, 15000); } //This method can be called concurrently by several threads private InputStream getResource(String uri) { try { HttpGet method = new HttpGet(uri); HttpResponse httpResponse = client.execute(method); int statusCode = httpResponse.getStatusLine().getStatusCode(); InputStream is = null; if (HttpStatus.SC_OK == statusCode) { logger.debug("200 OK Amazon request"); is = httpResponse.getEntity().getContent(); } else { logger.debug("Something went wrong, statusCode is {}", statusCode); EntityUtils.consume(httpResponse.getEntity()); } return is; } catch (Exception e) { logger.error("Something went terribly wrong", e); throw new RuntimeException(e); } } } 
+9
Feb 07 '13 at 22:50
source

This is a frequently asked question. The BalusC reaction is correct. Throw an HttpReponseException and raise an HttpResponseException. response . ignore (). If you need to read the error message, use the answer. parseAsString () if you do not know the content type of the response, otherwise if you do not know how to use the content type response. parseAs (MyType.class).

A simple snippet of code from YouTubeSample.java in youtube-jsonc-sample (although usually you want to do something smarter in a real application):

  } catch (HttpResponseException e) { System.err.println(e.response.parseAsString()); } 

Full disclosure: I am the owner of the google-api-java-client project.

+8
Jan 10 2018-11-11T00:
source

I had the same problem with jax-rs (resteasy) Response object in my unit tests. I solved this by calling response.releaseConnection(); The releaseConnection () method is only for the resteasy ClientResponse ClientResponse , so I had to add a listing from Response to ClientResponse .

+3
Oct 18 '12 at 9:00
source

try it

 HttpResponse response = Client.execute(httpGet); response.getEntity().consumeContent(); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == 200) { //task Log.i("Connection", "OK"); }else{ Log.i("Connection", "Down"); } 
+1
Jun 10. '14 at 6:02
source

Well, I have a similar problem, all these solutions do not work, I tested on some device, the problem was the date on the device, it was 2011, not 2013, check also that this can help.

0
Feb 08
source

Read the InputStream as follows:

 if( response.getStatusLine().getStatusCode() == 200 ) { HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); try { sb = new StringBuilder(); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( content ), 8 ); String line; while( ( line = bufferedReader.readLine() ) != null ) { sb.append( line ); } bufferedReader.close(); content.close(); } catch( Exception ex ) { Log.e( "statusCode", ex.getMessage() + "" ); } } 
0
Aug 24 '14 at 14:26
source

just consume the answer as below, this will solve the problem

 response.getEntity().consumeContent(); 
0
Jun 24 '19 at
source



All Articles