广西扶贫服务器响应超时,如果服务器没有响应,则超时不起作用

我遇到的问题是,当服务器没有响应时,我从未得到过SocketTimeOutException。 30秒后,我得到了一个IOException。

当服务器没有响应时,我需要做什么才能获得超时?

这是我的代码,其中第一个URL正常工作,第二个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);

}

}

}

我也尝试过使用aptt 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);

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值