httpClient检测url地址的状态
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
public class NetState {
public static int urlStatusLine(String url) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
if (response != null) {
return response.getStatusLine().getStatusCode();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return 0;
}
public static void main(String[] args) {
String url = "https://www.cnblogs.com/exmyth/p/6432261.html";
System.out.println(urlStatusLine(url));
}
}