hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Partheil <michael.parth...@gmx.de>
Subject Detecting lost network connection?!
Date Sun, 19 Aug 2007 19:46:20 GMT
Hi!

I'm having a problem with detecting when the computer my program is  
running on loses its connection to the internet/server while  
streaming a HTTP-Response Body.
To be concrete: I'm receiving a file via HTTP-GET and then simply  
disconnect the computer from the internet. My program stops receiving  
anything but does not throw a SocketTimeoutException or something  
similar although I've set all (?) the necessary parameters on the  
HttpClient and GetMethod (please see sample code below!). I thought  
that something like
client.getHttpConnectionManager().closeIdleConnections(10000); would  
do the job but it doesn't. In fact I see something like the following  
in the log?!
[DEBUG] IdleConnectionHandler - Checking for connections,  
idleTimeout: 1187551910312

The sample code below reproduces the problem: It simply sets up a  
HttpClient+GetMethod with an url representing an indefinitely large  
mp3 file (internet radio stream) and starts a Thread which reads  
data. If you then disconnect your computer the Thread doesn't stop  
with an exception.


Java version: 1.5.0_07, Apple Computer, Inc.
Operating system: Mac OS X 10.4.10
HttpClient version: 3.0.1


Thanks a lot!
Michael Partheil


===  Sample code:
public class HttpClientTest
{
	private static final String URL = "http:// 
d85195728.i.tis.core005.cdn.streamfarm.net:80/17000hr/live/3435hr3/ 
de_96.mp3";
	
	public static void main(String... args) throws Exception
	{
		HttpClient client = new HttpClient(new  
MultiThreadedHttpConnectionManager());
		client.getHttpConnectionManager().closeIdleConnections(10000);
		client.getHttpConnectionManager().getParams().setSoTimeout(5000);
		client.getParams().setSoTimeout(5000);
		client.getParams().setIntParameter(HttpClientParams.MAX_REDIRECTS,  
10);
		client.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
	
		GetMethod get = new GetMethod(URL);
		get.getParams().setSoTimeout(5000);
		get.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
		get.setFollowRedirects(true);
		get.addRequestHeader("User-Agent", "WinAmp");
		
		client.executeMethod(get);
		
		InputStream in = get.getResponseBodyAsStream();
		(new ReaderThread(in)).start();
	}
	
	private static class ReaderThread extends Thread
	{
		private final InputStream in;
		
		public ReaderThread(InputStream in)
		{
			this.in = in;
		}
		public void run()
		{
			try
			{
				while (! isInterrupted())
				{
					try
					{
						Thread.sleep(100);
					}
					catch (InterruptedException e)
					{
						interrupt();
						return;
					}

					try
					{
						int toRead = in.available();
						if (toRead == 0)
							continue;
						byte[] buffer = new byte[toRead];
						int nRead = in.read(buffer, 0, toRead);
						if (nRead == -1)
							return;
						assert nRead == toRead;
						
						System.out.println("read: "+nRead);
					}
					catch (IOException e)
					{
						e.printStackTrace();
						return;
					}
				}
			}
			finally
			{
				System.out.println("ReaderThread finished");
			}
		}
	}
}


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Mime
View raw message