Connection timeout problem using HttpClient
843790Jan 28 2009 — edited Jan 28 2009Hi,
I am using HttpClient to connect to another URL to read the data, where i am facing a problem with the connection timeout .
I am using JDK1.4,HttpClient 3.1. Here is the snippet of the code:-
public static void main(String[] args) {
System.getProperties().put("http.proxySet","true");
System.getProperties().put("http.proxyHost","http://proxypac.supervalu.com/proxy.pac");
System.getProperties().put("http.proxyPort","80");
System.setProperty("java.net.useSystemProxies", "false");
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Create a method instance.
GetMethod method = new GetMethod("http://www.google.com");
method.getParams().setParameter("http.socket.timeout", new Integer(10000));
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
}
I am pretty sure this is due to Proxy settings, eventhough i have set the proxy in the Java code still i am getting the same connection timeout problem.
If i have missed out something pls. do point out. Please do provide a solution to this.
Thanks,
JavaCrazy