Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HTTP connection timeout

31 views
Skip to first unread message

Dan Machak

unread,
Jan 14, 1999, 3:00:00 AM1/14/99
to
Hello, I'm looking for a way to timeout an
HttpURLConnection. What's happening, is with the
following block of code:

URL mURL = new URL("http://someserver/somefile");
HttpURLConnection mConnection =
(HttpURLConnection)mURL.openConnection();
mConnection.connect();
int mStatus = uConnect.getResponseCode();

I'm getting cases where the getResponsCode() method is
hanging when the server is up, but does not send data
for some reason. I was hoping that you could set a
timeout length in the HttpURLConnection object, but I
can't find anyway to do that. I know that the Socket
class has a way of setting a timeout, but it doesn't
handle the HTTP stream. I was using a separate thread to
make the connection, and having the main thread stop the
connecting thread if it took too long, but
Thread.suspend() and Thread.stop() have been deprecated
in Java 2, and Thread.interrupt won't work, because the
thread is hung in the getResponseCode() method.

Any suggestions on how I can solve this problem would be
helpful.

Thanks,
Dan Machak
mac...@nas.nasa.gov


Thomas Tuft Muller

unread,
Jan 15, 1999, 3:00:00 AM1/15/99
to

Dan Machak wrote in message <369E9A35...@nas.nasa.gov>...


stop, and suspend/resume can be simulated with wait/notify and conditional
variables.

-Thomas

orv...@weyrich.com

unread,
Jan 15, 1999, 3:00:00 AM1/15/99
to
I don't have an answer, but I can add to the problem :-/

I have the following code using sockets (JDK 1.1.7B on Win98)

import java.net.*;
import java.io.*;

public class LookForPorts
{
public static void main ( String[] args )
{
Socket theSocket;
String host = "localhost";

if ( args.length > 0 )
{
host = args[0];
}

for ( int i = 1; i < 1024; i++ )
{
try
{
theSocket = new Socket( host, i );
theSocket.setSoTimeout( 5 );
System.out.println( "Timeout = " +
theSocket.getSoTimeout() );

System.out.println( "Found server on port "
+ i + " of " + host );
}
catch ( IOException e )
{
System.err.println( e );
break;
}
}
}
}

The first problem is that the setSoTimeout( 5 ) method invocation
seems to do nothing -- the next getSoTimeout() call always returns 0.

The second problem is that if I provide as arg[0] a nonexistent
IP address (e.g. 198.49.110.150, which is assigned to me by Internic
but which has no host in operation) then the program hangs forever
without throwing an exception.

My first question is why doesn't the setSoTimeout seem to work.

My second question is how can I get the program to tell me that there
is no path to 198.49.110.150 when no such host I.P. exists?

TIA

orville.

In article <369E9A35...@nas.nasa.gov>,


Dan Machak <mac...@nas.nasa.gov> wrote:
> Hello, I'm looking for a way to timeout an
> HttpURLConnection. What's happening, is with the
> following block of code:
>
> URL mURL = new URL("http://someserver/somefile");
> HttpURLConnection mConnection =
> (HttpURLConnection)mURL.openConnection();
> mConnection.connect();
> int mStatus = uConnect.getResponseCode();
>
> I'm getting cases where the getResponsCode() method is
> hanging when the server is up, but does not send data
> for some reason. I was hoping that you could set a
> timeout length in the HttpURLConnection object, but I
> can't find anyway to do that. I know that the Socket
> class has a way of setting a timeout, but it doesn't
> handle the HTTP stream.

Weyrich Computer Consulting http://www.weyrich.com
Orville R. Weyrich, Jr. mailto:orv...@weyrich.com

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Dan Machak

unread,
Jan 15, 1999, 3:00:00 AM1/15/99
to
Thomas Tuft Muller wrote:

> stop, and suspend/resume can be simulated with wait/notify and conditional
> variables.
>
> -Thomas

This is true, but the problem is, the thread blocks on the getResponseCode()
method, and that is the method that hangs indefinately waiting for a response
from the server. Therefore the thread doesn't have a chance to detect whether
it should stop itself.

Dan
--


0 new messages