public static void main(String[] args) {
try {
String str = "https://www.baidu.com/";
URL url = new URL(str);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
System.out.println(responseCode);
} catch (Exception e) {
e.printStackTrace();
}
}
执行结果: