• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

java.net.ConnectException: Connection timed out in HttpClient

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 think I'll just lie down here for a second. And ponder this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic