+
+ // HttpURLConnection doesn't follow redirects from http to https. It should!
+ HttpURLConnection conn = followConnection(connection);
+ return conn.getResponseCode() == 200;
+ }
+
+ /**
+ * wrapper to return a new HttpURLConnection to a new URL when there is a
+ * redirect from http to https, otherwise return the unused original
+ * HttpURLConnection
+ *
+ * @param HttpURLConnection
+ * conn0
+ * @return HttpUrlConnection conn
+ */
+ public static HttpURLConnection followConnection(HttpURLConnection conn0)
+ throws IOException
+ {
+ return followConnection(conn0, false);
+ }
+
+ public static HttpURLConnection followConnection(HttpURLConnection conn0,
+ boolean followAnyway) throws IOException
+ {
+ URL url = conn0.getURL();
+ // we are only checking for a redirect from http to https otherwise the java
+ // connection will follow when called (if not unset)
+ if (url == null)
+ {
+ return conn0;
+ }
+ if (!conn0.getInstanceFollowRedirects())
+ {
+ return conn0;
+ }
+ if (!"http".equals(url.getProtocol()) && !followAnyway)
+ {
+ return conn0;
+ }
+
+ // check the response code
+ HttpURLConnection checkConn = (HttpURLConnection) url.openConnection();
+ httpURLConnectionCopyAttributes(conn0, checkConn);
+
+ boolean redirectToHttps = false;
+ int response = checkConn.getResponseCode();
+ checkConn.disconnect();
+ if (response >= 300 && response < 400)
+ {
+ // we are only checking for a redirect from http to https
+ URL loc = new URL(conn0.getHeaderField("Location"));
+ if (loc != null && "https".equals(loc.getProtocol()))
+ {
+ redirectToHttps = true;
+ url = loc;
+ }
+ }
+ else if (followAnyway)
+ {
+ // checkConn might have followed a https->https redirect
+ url = checkConn.getURL();
+ }
+
+ if (!redirectToHttps && !followAnyway)
+ {
+ return conn0;
+ }
+
+ // We want to return an HttpURLConnection to the new (probably https) URL
+ // that is unconnected in case further manipulation of the request is
+ // required.
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ httpURLConnectionCopyAttributes(conn0, conn);
+ return conn;
+ }
+
+ private static void httpURLConnectionCopyAttributes(
+ HttpURLConnection conn0, HttpURLConnection conn1)
+ throws ProtocolException
+ {
+ conn1.setRequestMethod(conn0.getRequestMethod());
+ conn1.setDoInput(conn0.getDoInput());
+ conn1.setUseCaches(conn0.getUseCaches());
+ conn1.setConnectTimeout(conn0.getConnectTimeout());
+ conn1.setReadTimeout(conn0.getReadTimeout());
+ conn1.setInstanceFollowRedirects(conn0.getInstanceFollowRedirects());
+ }
+
+ /**
+ * wrapper to follow a URL connection ALLOWING redirects from http to https
+ *
+ * @param URL
+ * url
+ * @return HttpUrlConnection conn
+ */
+ public static URLConnection openConnection(URL url) throws IOException
+ {
+ return openConnection(url, false);
+ }
+
+ public static URLConnection openConnection(URL url, boolean followAnyway)
+ throws IOException
+ {
+ if (url == null)
+ {
+ Console.debug("HttpUtils.openConnection(url) called with null url");
+ return null;
+ }
+ Console.debug("HttpUtils.openConnection(url) called with url="
+ + url.toString());
+ URLConnection conn = null;
+ String protocol = url.getProtocol();
+ if ("http".equals(protocol) || "https".equals(protocol))
+ {
+ HttpURLConnection conn0 = (HttpURLConnection) url.openConnection();
+ if (conn0 != null)
+ {
+ conn = HttpUtils.followConnection(conn0, followAnyway);
+ }
+ else
+ {
+ conn = conn0;
+ }
+ }
+ else
+ {
+ conn = url.openConnection();
+ }
+ return conn;