HttpClient hClient = new HttpClient();
hClient.getParams().setSoTimeout(25000);
// hClient.setTimeout(5000); deprecated in 3.0 client
HostConfiguration hc = hClient.getHostConfiguration();
hc = setProxySetttings(hClient, hc);
HeadMethod hMethod = null;
try {
hMethod = new HeadMethod(url.toString());
hMethod.setRequestHeader("cache-control", "no-cache");
int status = hClient.executeMethod(hMethod);
if (status != HttpStatus.SC_OK) {
// Check for redirection.
if (status == HttpStatus.SC_MOVED_PERMANENTLY
|| status == HttpStatus.SC_MOVED_TEMPORARILY
|| status == HttpStatus.SC_SEE_OTHER
|| status == HttpStatus.SC_TEMPORARY_REDIRECT) {
String redirectLocation;
Header locationHeader = hMethod
.getResponseHeader("location");
if (locationHeader != null) {
redirectLocation = locationHeader.getValue();
hMethod = new HeadMethod(redirectLocation);
status = hClient.executeMethod(hMethod);
if (status != HttpStatus.SC_OK) {
throw new NetworkException(hMethod.getStatusLine()
.getReasonPhrase());
}
} else {
// The response is invalid and did not provide the
// new
// location for
// the resource. Report an error or possibly handle
// the
// response
// like a 404 Not Found error.
}
} else {
if (status == HttpStatus.SC_UNAUTHORIZED) {
// Retry with password.
hc = hClient.getHostConfiguration();
try {
hClient.getState().setCredentials(
new AuthScope(AuthScope.ANY_HOST,
AuthScope.ANY_PORT,
AuthScope.ANY_REALM),
getCredentials(feed)
);
} catch (Exception e) {
throw new NetworkException(e.getMessage());
}
hMethod = new HeadMethod(url.toString());
hMethod.setDoAuthentication(true);
status = hClient.executeMethod(hMethod);
if (status != HttpStatus.SC_OK) {
throw new NetworkException(hMethod.getStatusLine()
.getReasonPhrase());
}
} else {
throw new NetworkException(hMethod.getStatusLine()
.getReasonPhrase());
}
}
}
head = getHeadInfo(hMethod);
} catch (HttpException he) {
// Received Illegal redirect exception on some URLs
throw new NetworkException(he.getMessage(), he);
} catch (IOException ioe) {
throw new NetworkException(ioe.getMessage(), ioe);
} catch (java.lang.IllegalArgumentException iae) {
throw new NetworkException(iae.getMessage(), iae);
} finally {
if (hMethod != null) {
hMethod.releaseConnection();
}
}
return head;
}