Ignore:
Timestamp:
2015-08-08T18:42:12+02:00 (9 years ago)
Author:
wiktorn
Message:

Follow HTTP(S) redirects in Add WMS wizzard / GetLayers. Closes: #11770

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r8602 r8650  
    895895
    896896    /**
     897     * Opens a HTTP connection to given URL, sets the User-Agent property to JOSM's one, optionally disables Keep-Alive, and
     898     * optionally - follows redirects. It means, that it's not possible to send custom headers with method
     899     *
     900     * @param httpURL The HTTP url to open (must use http:// or https://)
     901     * @param keepAlive whether not to set header {@code Connection=close}
     902     * @param followRedirects wheter or not to follow HTTP(S) redirects
     903     * @return An open HTTP connection to the given URL
     904     * @throws IOException if an I/O exception occurs
     905     * @since 8650
     906     */
     907    public static HttpURLConnection openHttpConnection(URL httpURL, boolean keepAlive, boolean followRedirects) throws IOException {
     908        HttpURLConnection connection = openHttpConnection(httpURL, keepAlive);
     909        if (followRedirects) {
     910            for (int i = 0; i < 5; i++) {
     911                if (connection.getResponseCode() == 302) {
     912                    connection = openHttpConnection(new URL(connection.getHeaderField("Location")), keepAlive);
     913                } else {
     914                    break;
     915                }
     916            }
     917        }
     918        return connection;
     919    }
     920
     921    /**
    897922     * An alternative to {@link String#trim()} to effectively remove all leading and trailing white characters, including Unicode ones.
    898923     * @param str The string to strip
Note: See TracChangeset for help on using the changeset viewer.