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

 

你可能感兴趣的:(httpclient检查某个链接是否可用)