码迷,mamicode.com
首页 > Web开发 > 详细

httpclient检查某个链接是否可用

时间:2015-08-13 19:47:38      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

private boolean checkUrlIsValid(String url) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(5000)
				.build();		
		HttpGet httpGet = new HttpGet(url);
		httpGet.setConfig(requestConfig);
		CloseableHttpResponse response = null;
		
		boolean isValid = false;
		
		try {
			response = httpClient.execute(httpGet);
			int statusCode = response.getStatusLine().getStatusCode();
			if(statusCode == 200) {
				isValid = true;
			}
		} catch (Exception e) {
			
		} finally {
			if(response != null) {
				try {
					response.close();
				} catch (IOException e) {
					
				}
			}
		}
		return isValid;
	}

 

httpclient检查某个链接是否可用

标签:

原文地址:http://www.cnblogs.com/zhangfei/p/4728182.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!