Changeset 9171 in josm for trunk/src/org/openstreetmap/josm/tools
- Timestamp:
- 2015-12-26T23:42:00+01:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/HttpClient.java
r9169 r9171 8 8 import java.io.IOException; 9 9 import java.io.InputStream; 10 import java.io.InputStreamReader;11 10 import java.io.OutputStream; 12 11 import java.net.HttpURLConnection; 13 12 import java.net.URL; 14 import java.nio.charset.StandardCharsets;15 13 import java.util.Map; 14 import java.util.Scanner; 16 15 import java.util.concurrent.ConcurrentHashMap; 17 16 import java.util.zip.GZIPInputStream; … … 20 19 import org.openstreetmap.josm.data.Version; 21 20 import org.openstreetmap.josm.io.Compression; 21 import org.openstreetmap.josm.io.UTFInputStreamReader; 22 22 23 23 /** … … 38 38 private final Map<String, String> headers = new ConcurrentHashMap<>(); 39 39 private int maxRedirects = Main.pref.getInteger("socket.maxredirects", 5); 40 private boolean useCache; 41 private boolean keepAlive; 40 42 41 43 private HttpClient(URL url, String requestMethod) { … … 70 72 connection.setIfModifiedSince(ifModifiedSince); 71 73 } 74 connection.setUseCaches(useCache); 75 if (!useCache) { 76 connection.setRequestProperty("Cache-Control", "no-cache"); 77 } 78 if (!keepAlive) { 79 connection.setRequestProperty("Connection", "close"); 80 } 72 81 for (Map.Entry<String, String> header : headers.entrySet()) { 73 82 connection.setRequestProperty(header.getKey(), header.getValue()); … … 78 87 try { 79 88 connection.connect(); 89 Main.info("{0} {1} => {2}", requestMethod, url, connection.getResponseCode()); 80 90 } catch (IOException e) { 81 91 //noinspection ThrowableResultOfMethodCallIgnored … … 157 167 158 168 /** 159 * Returns {@link #getContent()} wrapped in a buffered reader 169 * Returns {@link #getContent()} wrapped in a buffered reader. 170 * 171 * Detects Unicode charset in use utilizing {@link UTFInputStreamReader}. 160 172 */ 161 173 public BufferedReader getContentReader() throws IOException { 162 return new BufferedReader(new InputStreamReader(getContent(), StandardCharsets.UTF_8)); 174 return new BufferedReader( 175 UTFInputStreamReader.create(getContent()) 176 ); 177 } 178 179 /** 180 * Fetches the HTTP response as String. 181 * @return the response 182 * @throws IOException 183 */ 184 public String fetchContent() throws IOException { 185 try (Scanner scanner = new Scanner(getContentReader())) { 186 return scanner.useDelimiter("\\A").next(); 187 } 163 188 } 164 189 … … 187 212 188 213 /** 214 * Returns the {@code Content-Length} header. 215 */ 216 public long getContentLength() { 217 return connection.getContentLengthLong(); 218 } 219 220 /** 189 221 * @see HttpURLConnection#disconnect() 190 222 */ … … 213 245 public static HttpClient create(URL url, String requestMethod) { 214 246 return new HttpClient(url, requestMethod); 247 } 248 249 /** 250 * Sets whether not to set header {@code Cache-Control=no-cache} 251 * 252 * @param useCache whether not to set header {@code Cache-Control=no-cache} 253 * @return {@code this} 254 * @see HttpURLConnection#setUseCaches(boolean) 255 */ 256 public HttpClient useCache(boolean useCache) { 257 this.useCache = useCache; 258 return this; 259 } 260 261 /** 262 * Sets whether not to set header {@code Connection=close} 263 * <p/> 264 * This might fix #7640, see <a href='https://web.archive.org/web/20140118201501/http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive'>here</a>. 265 * 266 * @param keepAlive whether not to set header {@code Connection=close} 267 * @return {@code this} 268 */ 269 public HttpClient keepAlive(boolean keepAlive) { 270 this.keepAlive = keepAlive; 271 return this; 215 272 } 216 273
Note:
See TracChangeset
for help on using the changeset viewer.