使用 HttpClient 获取 URL 状态的最快方法是什么?我不想下载页面/文件,我只想知道页面/文件是否存在?(如果是重定向,我希望它跟随重定向)
问问题
14125 次
4 回答
14
以下是我从 HttpClient 获取状态码的方法,我非常喜欢:
public boolean exists(){
CloseableHttpResponse response = null;
try {
CloseableHttpClient client = HttpClients.createDefault();
HttpHead headReq = new HttpHead(this.uri);
response = client.execute(headReq);
StatusLine sl = response.getStatusLine();
switch (sl.getStatusCode()) {
case 404: return false;
default: return true;
}
} catch (Exception e) {
log.error("Error in HttpGroovySourse : "+e.getMessage(), e );
} finally {
try {
response.close();
} catch (Exception e) {
log.error("Error in HttpGroovySourse : "+e.getMessage(), e );
}
}
return false;
}
于 2014-03-20T18:12:11.760 回答
6
使用HEAD调用。它基本上是服务器不返回正文的 GET 调用。他们的文档中的示例:
HeadMethod head = new HeadMethod("http://jakarta.apache.org");
// execute the method and handle any error responses.
...
// Retrieve all the headers.
Header[] headers = head.getResponseHeaders();
// Retrieve just the last modified header value.
String lastModified = head.getResponseHeader("last-modified").getValue();
于 2012-08-27T16:37:33.093 回答
1
您可以通过以下方式获取此信息java.net.HttpURLConnection
:
URL url = new URL("http://stackoverflow.com/");
URLConnection urlConnection = url.openConnection();
if (urlConnection instanceof HttpURLConnection) {
int responseCode = ((HttpURLConnection) urlConnection).getResponseCode();
switch (responseCode) {
case HttpURLConnection.HTTP_OK:
// HTTP Status-Code 302: Temporary Redirect.
break;
case HttpURLConnection.HTTP_MOVED_TEMP:
// HTTP Status-Code 302: Temporary Redirect.
break;
case HttpURLConnection.HTTP_NOT_FOUND:
// HTTP Status-Code 404: Not Found.
break;
}
}
于 2012-08-28T12:00:51.073 回答
0
您可以使用:
HeadMethod head = new HeadMethod("http://www.myfootestsite.com");
head.setFollowRedirects(true);
// Header stuff
Header[] headers = head.getResponseHeaders();
请确保您的 Web 服务器支持 HEAD 命令。
请参阅HTTP 1.1 规范中的第 9.4 节
于 2012-08-27T17:01:36.167 回答