@@ -65,12 +65,16 @@ public final class HttpOpener extends DefaultObjectPipe<String, ObjectReceiver<R
65
65
public static final String HEADER_FIELD_SEPARATOR = "\n " ;
66
66
public static final String HEADER_VALUE_SEPARATOR = ":" ;
67
67
public static final String INPUT_DESIGNATOR = "@-" ;
68
+ public static final String MIME_PARAMETER_CHARSET = "charset" ;
69
+ public static final String MIME_PARAMETER_SEPARATOR = ";" ;
70
+ public static final String MIME_PARAMETER_VALUE_SEPARATOR = "=" ;
68
71
69
72
public static final String DEFAULT_METHOD_NAME = "GET" ;
70
73
public static final Method DEFAULT_METHOD = Method .valueOf (DEFAULT_METHOD_NAME );
71
74
72
75
private static final Pattern HEADER_FIELD_SEPARATOR_PATTERN = Pattern .compile (HEADER_FIELD_SEPARATOR );
73
76
private static final Pattern HEADER_VALUE_SEPARATOR_PATTERN = Pattern .compile (HEADER_VALUE_SEPARATOR );
77
+ private static final Pattern MIME_PARAMETER_SEPARATOR_PATTERN = Pattern .compile (MIME_PARAMETER_SEPARATOR );
74
78
75
79
private static final int ALLOWED_REDIRECTIONS = 3 ;
76
80
private static final int CONNECTION_TIMEOUT = 11000 ;
@@ -310,7 +314,7 @@ private Reader doPostOrPut(final String requestBody, final URL urlToOpen) throws
310
314
headers .forEach (connection ::setRequestProperty );
311
315
connection .getOutputStream ().write (requestBody .getBytes ());
312
316
final InputStream inputStream = getInputStream (connection );
313
- return new InputStreamReader (inputStream , headers . get ( ACCEPT_CHARSET_HEADER ));
317
+ return new InputStreamReader (inputStream , getContentCharset ( connection ));
314
318
}
315
319
316
320
private Reader doGet (final String requestUrl ) throws IOException {
@@ -321,10 +325,10 @@ private Reader doGet(final String requestUrl) throws IOException {
321
325
322
326
if ("gzip" .equalsIgnoreCase (connection .getContentEncoding ())) {
323
327
final GZIPInputStream gzipInputStream = new GZIPInputStream (inputStream );
324
- reader = new InputStreamReader (gzipInputStream );
328
+ reader = new InputStreamReader (gzipInputStream , getContentCharset ( connection ) );
325
329
}
326
330
else {
327
- reader = new InputStreamReader (inputStream , headers . get ( ACCEPT_CHARSET_HEADER ));
331
+ reader = new InputStreamReader (inputStream , getContentCharset ( connection ));
328
332
}
329
333
return reader ;
330
334
}
@@ -371,6 +375,25 @@ private InputStream getErrorStream(final InputStream errorStream) {
371
375
}
372
376
}
373
377
378
+ private String getContentCharset (final HttpURLConnection connection ) {
379
+ final String contentType = connection .getContentType ();
380
+
381
+ if (contentType != null ) {
382
+ final String [] parts = MIME_PARAMETER_SEPARATOR_PATTERN .split (contentType );
383
+
384
+ for (int i = 1 ; i < parts .length ; ++i ) {
385
+ final String parameter = parts [i ].trim ();
386
+ final int index = parameter .indexOf (MIME_PARAMETER_VALUE_SEPARATOR );
387
+
388
+ if (index != -1 && MIME_PARAMETER_CHARSET .equalsIgnoreCase (parameter .substring (0 , index ))) {
389
+ return parameter .substring (index + 1 );
390
+ }
391
+ }
392
+ }
393
+
394
+ return CHARSET_DEFAULT ;
395
+ }
396
+
374
397
private HttpURLConnection followRedirects (final URL startingUrl ) throws IOException {
375
398
int times = 0 ;
376
399
HttpURLConnection conn ;
0 commit comments