来自 UrlConnection Java 的响应
Response from UrlConnection Java
到目前为止,我有这个片段。
URLConnection connection = null;
try {
connection = (new URL("some_link")).openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
} catch (IOException e) {
}
可能的响应代码是 200 和 404,当响应代码是 200 (OK) 时它工作正常。我的问题是如何找到我的连接收到的响应代码,例如:如果响应代码是 404,则抛出异常并在那里执行 smth。
使其成为HTTPUrlConnection
的类型。然后你可以使用 .getResponseCode();
使用这样的东西:
HttpURLConnection connection = (HttpURLConnection)new URL("URL_STRING")
.openConnection();
int statusCode = connection.getResponseCode();
到目前为止,我有这个片段。
URLConnection connection = null;
try {
connection = (new URL("some_link")).openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
} catch (IOException e) {
}
可能的响应代码是 200 和 404,当响应代码是 200 (OK) 时它工作正常。我的问题是如何找到我的连接收到的响应代码,例如:如果响应代码是 404,则抛出异常并在那里执行 smth。
使其成为HTTPUrlConnection
的类型。然后你可以使用 .getResponseCode();
使用这样的东西:
HttpURLConnection connection = (HttpURLConnection)new URL("URL_STRING")
.openConnection();
int statusCode = connection.getResponseCode();