Java 如果服务器没有响应,超时将不起作用
我的问题是,当服务器没有响应时,我永远不会得到SocketTimeOutException。30秒后,我得到了一个IOException 当服务器没有响应时,我需要做什么来获得超时Java 如果服务器没有响应,超时将不起作用,java,http,timeout,connection,Java,Http,Timeout,Connection,我的问题是,当服务器没有响应时,我永远不会得到SocketTimeOutException。30秒后,我得到了一个IOException 当服务器没有响应时,我需要做什么来获得超时 这是我的代码,其中第一个URL工作,第二个导致IOException而不是SocketTimeOutException 公共类TestConnection{ public static final int TIMEOUT_VALUE = 5000; public static void main(String[]
这是我的代码,其中第一个URL工作,第二个导致IOException而不是SocketTimeOutException 公共类TestConnection{
public static final int TIMEOUT_VALUE = 5000;
public static void main(String[] arg){
try {
URL testUrl = new URL("http://www.doublegames.com/images/games140/scrabble-cubes-online_140x140.jpg");
testTimeOut(testUrl);
testUrl = new URL("http://tamblang.co.cc/wp-content/uploads/2010/08/cd7ce2_command-038-conquer-4-tiberian-twilight-140x140.jpg");
testTimeOut(testUrl);
} catch (MalformedURLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
private static void testTimeOut(URL testUrl) {
try {
long start = System.nanoTime();
HttpURLConnection testConnection = (HttpURLConnection) testUrl.openConnection();
testConnection.setConnectTimeout(TIMEOUT_VALUE);
testConnection.setReadTimeout(TIMEOUT_VALUE);
BufferedReader in = new BufferedReader(new InputStreamReader(testConnection.getInputStream()));
long elapsed = System.nanoTime() - start;
System.out.println("Elapsed (ms): " + elapsed / 1000000);
System.out.println("Connection worked for " + testUrl);
} catch (SocketTimeoutException e) {
System.out.println("More than " + TIMEOUT_VALUE + " elapsed." + testUrl);
} catch(IOException ioe){
System.out.println("No timeout for " + testUrl);
}
}
}
我也尝试过apaches HttpClient,但也有同样的东西
private static void testTimeOut(URL testUrl) {
long start = 0;
try {
start = System.nanoTime();
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_VALUE);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_VALUE);
HttpClient httpClient = new DefaultHttpClient(httpParams);
HttpGet httpget = new HttpGet(testUrl.toString());
HttpResponse response = httpClient.execute(httpget);
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to bother about connection release
if (entity != null) {
InputStream instream = entity.getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(instream));
// do something useful with the response
long elapsed = System.nanoTime() - start;
System.out.println("Elapsed (ms): " + elapsed / 1000000);
System.out.println("Connection worked for " + testUrl);
}
}catch(Exception e){
long elapsed = System.nanoTime() - start;
System.out.println("Elapsed (ms): " + elapsed / 1000000);
System.out.println("No timeout for " + testUrl + " after " + elapsed / 100000);
}
}
您正试图通过
testUrl.openConnection()
打开连接,其中testUrl
的类型为URL
。好的,如果您查看一下特定的,您会注意到URL.openConnection()
永远不会抛出SocketTimeoutException
。它只能抛出一个IOException
您可以捕获IOException并在catch块中抛出新的SocketTimeOutException
能否将以下内容添加到IOException捕获中:
System.err.println(e.getMessage());e、 printStackTrace()代码>“第二次导致IOException”。什么例外?永远不要忘记在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)的java.net.socksocketImpl.connect(socksocketImpl.java:432)的java.net.Socket.connect(socksocket.java:529)的sun.net.NetworkClient.doConnect(NetworkClient.java:158)的java.Socket.co.cc中记录或打印一个异常FYISocketTimeoutException
扩展自IOException
。大多数抛出SocketTimeoutException
的Java代码只记录为IOException
。不管我遇到什么异常,我试图实现的是在5秒后启动超时。是的,但我希望超时在5秒后导致SocketTimeoutException,现在我在30秒后得到一个IOException。你可以启动一个线程/计时器,并向其中添加一个侦听器。当它在5秒后过期时,你可以抛出SocketTimeoutException。我认为这听起来是最好的解决方案,但用任何其他方法都无法解决它。你有代码示例吗?我在考虑一个执行者/未来的解决方案。