简体   繁体   中英

HTTP connection with timeout under Java 1.5

I have developed a simple low-level access to a web service by using java.net.HttpURLConnection.

I need to set a timeout for connection and reading, and this seems to be possible under Java 1.6 and up, but for Java 1.5 I'm completely at a loss.

Is there any replacement for HttpURLConnection that can serve my needs?

My code right now is like this:

        oURL = new URL( this.endpoint );
        httpCon = (HttpURLConnection)oURL.openConnection();

        httpCon.setDoOutput(true);
        httpCon.setDoInput(true);

        httpCon.setConnectTimeout( this.connect_timeout );
        httpCon.setReadTimeout( this.read_timeout );

        httpCon.setRequestMethod("POST");
        httpCon.setRequestProperty("Content-type", "text/xml; charset=utf-8");
        httpCon.setRequestProperty("SOAPAction", this.endpoint+"#"+req.getOperationName());


        reqStream = httpCon.getOutputStream();
        reqStream.write( reqXML.getBytes());

Once I read the response from the output stream, I'm done. However, the implementation of HttpURLConnection under 1.5 seems to lack the two relevant methods.

I think you're not using JDK 1.5, I created a java (1.5) project in eclipse and it allows me to use both methods.

    HttpURLConnection a = (HttpURLConnection)new URL("URL").openConnection();
    a.setConnectTimeout(0);
    a.setReadTimeout(0);

and in the description of the methods it says it's since 1.5

setConnectTimeout

Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.

Some non-standard implementation of this method may ignore the specified timeout. To see the connect timeout set, please call getConnectTimeout().

Parameters: timeout an int that specifies the connect timeout value in milliseconds Throws: IllegalArgumentException - if the timeout parameter is negative Since: 1.5

setReadTimeout

Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.

Some non-standard implementation of this method ignores the specified timeout. To see the read timeout set, please call getReadTimeout().

Parameters: timeout an int that specifies the timeout value to be used in milliseconds Throws: IllegalArgumentException - if the timeout parameter is negative Since: 1.5

You can also see the API docs from oracle

http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLConnection.html#setConnectTimeout(int)

http://docs.oracle.com/javase/1.5.0/docs/api/java/net/HttpURLConnection.html

It says HttpURLConnection inherits from URLConnection and the two methods are public so they are in the HttpURLConnection class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM