|
8 | 8 | import java.io.BufferedReader;
|
9 | 9 | import java.io.InputStream;
|
10 | 10 | import java.io.InputStreamReader;
|
11 |
| -import java.net.URI; |
12 |
| -import java.net.URL; |
13 |
| -import java.net.URLConnection; |
14 |
| -import java.net.URLEncoder; |
| 11 | +import java.net.*; |
15 | 12 | import java.nio.charset.Charset;
|
16 | 13 | import java.nio.charset.StandardCharsets;
|
17 | 14 | import java.security.KeyManagementException;
|
@@ -95,42 +92,47 @@ public static String urlToString(String url, List<AuthorizationValue> auths) thr
|
95 | 92 | BufferedReader br = null;
|
96 | 93 |
|
97 | 94 | try {
|
98 |
| - final URL inUrl = new URL(cleanUrl(url)); |
99 |
| - final List<AuthorizationValue> query = new ArrayList<>(); |
100 |
| - final List<AuthorizationValue> header = new ArrayList<>(); |
101 |
| - if (auths != null) { |
102 |
| - for (AuthorizationValue auth : auths) { |
103 |
| - if ("query".equals(auth.getType())) { |
104 |
| - appendValue(inUrl, auth, query); |
105 |
| - } else if ("header".equals(auth.getType())) { |
106 |
| - appendValue(inUrl, auth, header); |
| 95 | + URLConnection conn; |
| 96 | + do { |
| 97 | + final URL inUrl = new URL(cleanUrl(url)); |
| 98 | + final List<AuthorizationValue> query = new ArrayList<>(); |
| 99 | + final List<AuthorizationValue> header = new ArrayList<>(); |
| 100 | + if (auths != null) { |
| 101 | + for (AuthorizationValue auth : auths) { |
| 102 | + if ("query".equals(auth.getType())) { |
| 103 | + appendValue(inUrl, auth, query); |
| 104 | + } else if ("header".equals(auth.getType())) { |
| 105 | + appendValue(inUrl, auth, header); |
| 106 | + } |
107 | 107 | }
|
108 | 108 | }
|
109 |
| - } |
110 |
| - final URLConnection conn; |
111 |
| - if (!query.isEmpty()) { |
112 |
| - final URI inUri = inUrl.toURI(); |
113 |
| - final StringBuilder newQuery = new StringBuilder(inUri.getQuery() == null ? "" : inUri.getQuery()); |
114 |
| - for (AuthorizationValue item : query) { |
115 |
| - if (newQuery.length() > 0) { |
116 |
| - newQuery.append("&"); |
| 109 | + if (!query.isEmpty()) { |
| 110 | + final URI inUri = inUrl.toURI(); |
| 111 | + final StringBuilder newQuery = new StringBuilder(inUri.getQuery() == null ? "" : inUri.getQuery()); |
| 112 | + for (AuthorizationValue item : query) { |
| 113 | + if (newQuery.length() > 0) { |
| 114 | + newQuery.append("&"); |
| 115 | + } |
| 116 | + newQuery.append(URLEncoder.encode(item.getKeyName(), UTF_8.name())).append("=") |
| 117 | + .append(URLEncoder.encode(item.getValue(), UTF_8.name())); |
117 | 118 | }
|
118 |
| - newQuery.append(URLEncoder.encode(item.getKeyName(), UTF_8.name())).append("=") |
119 |
| - .append(URLEncoder.encode(item.getValue(), UTF_8.name())); |
| 119 | + conn = new URI(inUri.getScheme(), inUri.getAuthority(), inUri.getPath(), newQuery.toString(), |
| 120 | + inUri.getFragment()).toURL().openConnection(); |
| 121 | + } else { |
| 122 | + conn = inUrl.openConnection(); |
120 | 123 | }
|
121 |
| - conn = new URI(inUri.getScheme(), inUri.getAuthority(), inUri.getPath(), newQuery.toString(), |
122 |
| - inUri.getFragment()).toURL().openConnection(); |
123 |
| - } else { |
124 |
| - conn = inUrl.openConnection(); |
125 |
| - } |
126 |
| - CONNECTION_CONFIGURATOR.process(conn); |
127 |
| - for (AuthorizationValue item : header) { |
128 |
| - conn.setRequestProperty(item.getKeyName(), item.getValue()); |
129 |
| - } |
| 124 | + CONNECTION_CONFIGURATOR.process(conn); |
| 125 | + for (AuthorizationValue item : header) { |
| 126 | + conn.setRequestProperty(item.getKeyName(), item.getValue()); |
| 127 | + } |
| 128 | + |
| 129 | + conn.setRequestProperty("Accept", ACCEPT_HEADER_VALUE); |
| 130 | + conn.setRequestProperty("User-Agent", USER_AGENT_HEADER_VALUE); |
| 131 | + |
| 132 | + conn.connect(); |
| 133 | + url = ((HttpURLConnection) conn).getHeaderField("Location"); |
| 134 | + } while (301 == ((HttpURLConnection) conn).getResponseCode()); |
130 | 135 |
|
131 |
| - conn.setRequestProperty("Accept", ACCEPT_HEADER_VALUE); |
132 |
| - conn.setRequestProperty("User-Agent", USER_AGENT_HEADER_VALUE); |
133 |
| - conn.connect(); |
134 | 136 | InputStream in = conn.getInputStream();
|
135 | 137 |
|
136 | 138 | StringBuilder contents = new StringBuilder();
|
|
0 commit comments