Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timeout not supported for HttpURLConnection in Java1.4.0 1

Status
Not open for further replies.

SwapSawe

Programmer
Apr 24, 2001
148
0
0
US
Hi All !!!

I am trying to create a program to connect to some server wherein, I intend to implement time out. However I have observed that this feature is supported by Java5.0 API, but sadly its missing in Java 1.4.x, which happens to be my production environment.

If any of you guys could direct me to a hint, it would be very helpful.

My Sample Code follows -

Code:
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class CheckHTTPURLConnection {

	public static void main(String args[]) {
	long t0=0;
	try {
	  URL u = new URL("[URL unfurl="true"]http://kbs.cs.tu-berlin.de/~jutta/ht/responses.html");[/URL]
	  HttpURLConnection huc = (HttpURLConnection) u.openConnection();
	  huc.setRequestMethod("TRACE");
	  huc.setDoInput(true);
	  huc.setDoOutput(true);
/* Does not work
          huc.setConnectTimeout(3000);
*/

	  t0= System.currentTimeMillis();
	  huc.connect();
	  OutputStream os = huc.getOutputStream();
	  int code = huc.getResponseCode();
	  System.out.println("The Returned code is :"+code);
	  if (code == 200 ) {
		System.out.println("Got the connection");
	  }
	  huc.disconnect();
	}
	catch(java.net.SocketTimeoutException ste) {
/* Would never be thrown as setConnectTimeout not supported in Java 1.4.x
		System.out.println("Time Limit Exceeded.");
	}

	catch (IOException e) {
		System.out.println("Connection Failed : Why? A:"+e);
	}
	  long t1= System.currentTimeMillis();
	  System.out.println("Total time taken :"+(t1-t0));
  }
}

Thanks in advance,

SwapSawe.

Cheer Up, The worst is yet to Come.
 
Hi Tim,

Thanx for everything. Follows the code that works, it needs few jars -commons-httpclient, commons-logging, commons-codec.

SwapSawe.
Code:
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.*;

public class HttpClientTutorial {

  private static String url = "[URL unfurl="true"]http://www.apache.org/";[/URL]

  public static void main(String[] args) {
    // Create an instance of HttpClient.
    HttpClient client = new HttpClient();

    // Create a method instance.
    GetMethod method = new GetMethod(url);

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
    		new DefaultHttpMethodRetryHandler(3, false));
    System.out.println("Setting time out to 10");
    method.getParams().setSoTimeout(10); // Throws IOException on TimeOut.


    try {
      // Execute the method.
	    System.out.println("Now exec method");

      int statusCode = client.executeMethod(method);
      System.out.println("The StatusCode is :"+statusCode);

      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();
    }
  }
}

Cheer Up, The worst is yet to Come.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top