This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Services and the fly likes java.net.ConnectException: Connection timed out in HttpClient Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "java.net.ConnectException: Connection timed out in HttpClient" Watch "java.net.ConnectException: Connection timed out in HttpClient" New topic
Author

java.net.ConnectException: Connection timed out in HttpClient

Manju Havald
Greenhorn

Joined: Mar 13, 2012
Posts: 1
I’m getting Connection timed out after processing about 20 records in HttpClient (code snippet is shown below). My requirement is to process about 30 records one after the other and read the response for each request.
Please let me known how to fix this limitation.

Code:
---------
import java.io.IOException;
import java.net.MalformedURLException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

public class Consumer {

public static void main(String[] args) {

Consumer consumer = new Consumer();
DefaultHttpClient httpClient = consumer.getHttpClientConnection("userId", "encrypted Password");
HttpPost postMethod = new HttpPost("https://destination.com/.../serviceName");

int loopCount = 30; // I need to send about 30 records one after the other. Here i kept loop count of 30 for testing purpose
for (int i = 0; i < loopCount; i++) {
System.out.println("Processing loop[" + i +"]");
consumer.sendHttpPostRequest(httpClient, postMethod);
}
consumer.closeHttpClientConnection(httpClient);
}

public DefaultHttpClient getHttpClientConnection(String userId, String encryptedPwd) {

HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 0);
params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 0);
params.setParameter(CoreConnectionPNames.TCP_NODELAY, true);
DefaultHttpClient httpClient = new DefaultHttpClient(params);

httpClient.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials(userId, encryptedPwd));

return httpClient;
}

public void sendHttpPostRequest(DefaultHttpClient httpClient,
HttpPost postMethod) {

try {
HttpContext localContext = new BasicHttpContext();
HttpResponse response = httpClient.execute(postMethod, localContext);
HttpEntity resEntity = response.getEntity();
EntityUtils.consume(resEntity);
} catch (MalformedURLException e) {
postMethod.abort();
e.printStackTrace();
} catch (IOException e) {
postMethod.abort();
e.printStackTrace();
}
}

public void closeHttpClientConnection(DefaultHttpClient httpClient) {
httpClient.getConnectionManager().closeExpiredConnections();
httpClient.getConnectionManager().shutdown();
System.gc();
}
}


Exception:
----------------
org.apache.http.conn.HttpHostConnectException: Connection to https://.....com refused
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:158)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:150)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:575)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
..........
.........
Caused by: java.net.ConnectException: Connection timed out: 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 com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(Unknown Source)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:375)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148)
... 10 more
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.net.ConnectException: Connection timed out in HttpClient
 
Similar Threads
Apache HTTP Components SSL Request
Webdav
HttpClient issue on Https Connection (multiple post)
Any way to overcome Circular Redirect using HttpClient 4.1.1
HttpClient Exception