I am having an issue with HttpClient 4.01. When I try to access
www.google.com or any other website I get a "java.net.SocketException:
Permission denied: connect". There is no firewall or other software blocking
Java and there are no proxies involved.

Here is the stack trace:

2010-06-23 21:42:57,803 [main] DEBUG
org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager -
ThreadSafeClientConnManager.getConnection:
HttpRoute[{}->http://www.google.com], timeout = 15000
2010-06-23 21:42:57,806 [main] DEBUG
org.apache.http.impl.conn.tsccm.ConnPoolByRoute - Total connections kept
alive: 0
2010-06-23 21:42:57,807 [main] DEBUG
org.apache.http.impl.conn.tsccm.ConnPoolByRoute - Total issued connections:
0
2010-06-23 21:42:57,808 [main] DEBUG
org.apache.http.impl.conn.tsccm.ConnPoolByRoute - Total allocated
connection: 0 out of 5
2010-06-23 21:42:57,810 [main] DEBUG
org.apache.http.impl.conn.tsccm.ConnPoolByRoute - No free connections
[HttpRoute[{}->http://www.google.com]][null]
2010-06-23 21:42:57,810 [main] DEBUG
org.apache.http.impl.conn.tsccm.ConnPoolByRoute - Available capacity: 2 out
of 2 [HttpRoute[{}->http://www.google.com]][null]
2010-06-23 21:42:57,811 [main] DEBUG
org.apache.http.impl.conn.tsccm.ConnPoolByRoute - Creating new connection
[HttpRoute[{}->http://www.google.com]]
2010-06-23 21:42:58,126 [main] DEBUG
org.apache.http.impl.conn.DefaultClientConnection - Connection shut down
2010-06-23 21:42:58,127 [main] DEBUG
org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager - Released
connection is not reusable.
2010-06-23 21:42:58,128 [main] DEBUG
org.apache.http.impl.conn.tsccm.ConnPoolByRoute - Releasing connection
[HttpRoute[{}->http://www.google.com]][null]
2010-06-23 21:42:58,128 [main] DEBUG
org.apache.http.impl.conn.tsccm.ConnPoolByRoute - Notifying no-one, there
are no waiting threads
java.net.SocketException: Permission denied: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(Unknown Source)
        at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at
org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:123)
        at
org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:123)
        at
org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:147)
        at
org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:108)
        at
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415)
        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)

Here is my HttpClient code:

       HttpParams params = new BasicHttpParams();
        ConnManagerParams.setMaxTotalConnections(params, 5);
        ConnManagerParams.setTimeout(params, 15 * 1000);
        HttpConnectionParams.setStaleCheckingEnabled(params, true);
        HttpConnectionParams.setConnectionTimeout(params, 15 * 1000);
        HttpConnectionParams.setSoTimeout(params, 20 * 1000);
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

        // Create and initialize scheme registry
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http",
PlainSocketFactory.getSocketFactory(), 80));
        schemeRegistry.register(new Scheme("https",
SSLSocketFactory.getSocketFactory(), 443));

        // Create an HttpClient with the ThreadSafeClientConnManager.
        // This connection manager must be used if more than one thread will
        // be using the HttpClient.
        ClientConnectionManager cm = new ThreadSafeClientConnManager(params,
schemeRegistry);

        httpclient = new DefaultHttpClient(cm, params);

        httpclient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {

            @Override
            public long getKeepAliveDuration(HttpResponse response,
HttpContext context) {
                // default keep alive for 100 seconds
                long keepAlive = 100 * 1000;

                // Honor 'keep-alive' header
                HeaderElementIterator it = new BasicHeaderElementIterator(
                        response.headerIterator(HTTP.CONN_KEEP_ALIVE));
                while (it.hasNext()) {
                    HeaderElement he = it.nextElement();
                    String param = he.getName();
                    String value = he.getValue();
                    if (value != null && param.equalsIgnoreCase("timeout"))
{
                        try {
                            keepAlive = Long.parseLong(value) * 1000;
                            break;
                        } catch (NumberFormatException ex) {
                            if (log.isInfoEnabled()) {
                                log.warn("Unable to parse timeout within
http header", ex);
                            }
                        }
                    }
                }
                // if you want custom keep-alive for a specific target
                //HttpHost target = (HttpHost)
context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

                return keepAlive;
            }
        });

        httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler()
{

            @Override
            public boolean retryRequest(
                    IOException exception,
                    int executionCount,
                    HttpContext context) {
                if (executionCount >= 5) {
                    // Do not retry if over max retry count
                    return false;
                }
                if (exception instanceof NoHttpResponseException) {
                    // Retry if the server dropped connection on us
                    return true;
                }
                if (exception instanceof SSLHandshakeException) {
                    // Do not retry on SSL handshake exception
                    return false;
                }
                HttpRequest request = (HttpRequest) context.getAttribute(
                        ExecutionContext.HTTP_REQUEST);
                boolean idempotent = !(request instanceof
HttpEntityEnclosingRequest);
                if (idempotent) {
                    // Retry if the request is considered idempotent
                    return true;
                }
                return false;
            }
        });

And the get request code:

        final HttpGet httpget = new HttpGet(URL);
        HttpResponse response = null;
        HttpEntity entity = null;
        String entityContent = null;

        try {
            response = httpclient.execute(httpget);
            entity = response.getEntity();

            if (entity != null) {
                entity.consumeContent();
            }
        } catch (UnknownHostException ex) {
            httpget.abort();
            entityContent = null;
            if (log.isDebugEnabled()) {
                log.debug("Unable to reach host", ex);
            }
        } catch (IOException ex) {
            httpget.abort();
            entityContent = null;
            log.error("Unable to consume entity content", ex);
        } catch (RuntimeException ex) {
            // In case of an unexpected exception you may want to abort
            // the HTTP request in order to shut down the underlying
            // connection and release it back to the connection manager.
            httpget.abort();
            entityContent = null;
            log.error("Unexpected exception during runtime", ex);
        }
-- 
View this message in context: 
http://old.nabble.com/Connection-Shutdown-Immediately-on-HttpClient-4.01-tp28978594p28978594.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to