httpclient检查某个链接是否可用

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;
	}

 

posted on 2015-08-13 18:51  张飞_  阅读(2940)  评论(0编辑  收藏  举报

导航