NotFound
HelloHttpClientNotFoundMain
存在しないファイルURLにアクセスしたとき
■コード
package hello.org.apache.commons.httpclient;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
public class HelloHttpClientNotFoundMain {
public static void main(String[] args) {
String urlNotExists = "http://www.apache.org/notexists";
HttpClient client = new HttpClient();
// not throws Exception but response is 404
HttpMethod method = new GetMethod(urlNotExists);
try {
int res = client.executeMethod(method);
if (res == HttpStatus.SC_NOT_FOUND) {
System.err.println("not found.");
}
byte[] responseBody = method.getResponseBody();
System.out.println(new String(responseBody));
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
method.releaseConnection();
client.getHttpConnectionManager().closeIdleConnections(0);
}
}
}
■結果
not found.
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /notexists was not found on this server.</p>
<hr>
<address>Apache/2.3.8 (Unix) mod_ssl/2.3.8 OpenSSL/1.0.0a Server at www.apache.org Port 80</address>
</body></html>