如何在Java中获取URL的HTTP响应代码?
2022-08-31 07:20:55
请告诉我获取特定URL的响应代码的步骤或代码。
URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
这绝不是一个有力的例子。你需要处理s等等。但它应该让你开始。IOException
如果您需要具有更多功能的内容,请查看HttpClient。
URL url = new URL("http://www.google.com/humans.txt");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
int statusCode = http.getResponseCode();