hale
hale copied to clipboard
Follow HTTP 301/302 redirects when opening URLs
With the widespread deployment of SSL certificates, plain http
connections get redirected to https
more and more often. The method
URL url = new URL("http://www.example.org");
InputStream is = url.openStream();
will return an empty InputStream
if e.g. http://www.example.org
redirects to https://www.example.org
.
An approach to follow redirects might be to something like
HttpURLConnection c = (HttpURLConnection) url.openConnection();
Set<String> visitedUrls = new HashSet<>();
boolean doneRedirecting = false;
while (!doneRedirecting) {
switch (c.getResponseCode()) {
case HttpURLConnection.HTTP_MOVED_PERM:
case HttpURLConnection.HTTP_MOVED_TEMP:
// Follow redirect if not already visisted
String newLocation = c.getHeaderField("Location");
if (visitedUrls.contains(newLocation)) {
throw new RuntimeException(MessageFormat.format(
"Infinite redirect loop detected for URL {0}", usedUrl.toString()));
}
visitedUrls.add(newLocation);
url = new URL(newLocation);
c = (HttpURLConnection) url.openConnection();
break;
default:
doneRedirecting = true;
break;
}
}
try (InputStream is = c.getInputStream()) {
// something
}
There are also redirects implemented for the INSPIRE schemas which leads to the fact that the XML validation fails when exporting data using an http URI (e.g. http://inspire.ec.europa.eu/schemas/hh/4.0/HumanHealth.xsd) I got a validation error when exporting HH data, but not when exporting SO data.