我有問題,當服務器沒有響應時,我從來沒有得到SocketTimeOutException。 30秒後,我得到一個IOException。如果服務器沒有響應,則超時不起作用
當服務器沒有響應時,我需要做些什麼來獲得超時?
這是我的代碼,其中第一個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);
}
}
}
我也試圖與阿帕奇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);
}
}
你可以添加以下到IOException異常陷阱:'通信System.err.println(e.getMessage()); e.printStackTrace();' – Bringer128
「第二次導致IOException」。 *什麼* IOException?永遠不會記錄或打印異常。 – EJP
的java.net.UnknownHostException:tamblang.co.cc \t在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195) \t在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432) \t在java的.net.Socket.connect(Socket.java:529) \t at sun.net.NetworkClient.doConnect(NetworkClient.java:158) – Patrik