hale icon indicating copy to clipboard operation
hale copied to clipboard

Follow HTTP 301/302 redirects when opening URLs

Open florianesser opened this issue 6 years ago • 3 comments

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
}

florianesser avatar Nov 29 '18 16:11 florianesser

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.

JohannaOtt avatar Mar 03 '21 13:03 JohannaOtt