|
18 | 18 | import java.io.IOException;
|
19 | 19 | import java.net.SocketTimeoutException;
|
20 | 20 | import java.nio.ByteBuffer;
|
| 21 | +import java.nio.charset.Charset; |
| 22 | +import java.nio.charset.StandardCharsets; |
21 | 23 | import java.util.ArrayList;
|
22 | 24 | import java.util.Collections;
|
23 | 25 | import java.util.Comparator;
|
| 26 | +import java.util.Iterator; |
24 | 27 | import java.util.UUID;
|
25 | 28 | import java.util.concurrent.ConcurrentHashMap;
|
26 | 29 | import java.util.concurrent.Executor;
|
@@ -97,6 +100,7 @@ public static class FileResult {
|
97 | 100 | }
|
98 | 101 | }
|
99 | 102 |
|
| 103 | + |
100 | 104 | static class ProgressRequestBody extends RequestBody {
|
101 | 105 | RequestBody body;
|
102 | 106 | ProgressListener listener;
|
@@ -354,19 +358,78 @@ public Request authenticate(Route route, Response response) throws IOException {
|
354 | 358 | if (options.content instanceof File) {
|
355 | 359 |
|
356 | 360 | } else if (options.content instanceof String) {
|
357 |
| - body = new ProgressRequestBody(RequestBody.create(MediaType.parse(contentType), (String) options.content), new ProgressListener() { |
358 |
| - @Override |
359 |
| - public void onProgress(long loaded, long total) { |
360 |
| - callback.onProgress(total > -1, loaded, total); |
| 361 | + if (contentType.equals("application/x-www-form-urlencoded")) { |
| 362 | + JSONTokener tokener = new JSONTokener((String) options.content); |
| 363 | + Object value = null; |
| 364 | + try { |
| 365 | + value = tokener.nextValue(); |
| 366 | + } catch (JSONException e) { |
| 367 | + e.printStackTrace(); |
361 | 368 | }
|
362 |
| - }); |
| 369 | + if (value instanceof JSONObject) { |
| 370 | + FormBody.Builder formBody = new FormBody.Builder(StandardCharsets.UTF_8); |
| 371 | + |
| 372 | + for (Iterator<String> it = ((JSONObject) value).keys(); it.hasNext(); ) { |
| 373 | + String key = it.next(); |
| 374 | + formBody.addEncoded(key, String.valueOf(((JSONObject) value).opt(key))); |
| 375 | + } |
| 376 | + body = new ProgressRequestBody(formBody.build(), new ProgressListener() { |
| 377 | + @Override |
| 378 | + public void onProgress(long loaded, long total) { |
| 379 | + callback.onProgress(total > -1, loaded, total); |
| 380 | + } |
| 381 | + }); |
| 382 | + }else { |
| 383 | + body = new ProgressRequestBody(RequestBody.create(MediaType.parse(contentType), (String) options.content), new ProgressListener() { |
| 384 | + @Override |
| 385 | + public void onProgress(long loaded, long total) { |
| 386 | + callback.onProgress(total > -1, loaded, total); |
| 387 | + } |
| 388 | + }); |
| 389 | + } |
| 390 | + |
| 391 | + } else { |
| 392 | + body = new ProgressRequestBody(RequestBody.create(MediaType.parse(contentType), (String) options.content), new ProgressListener() { |
| 393 | + @Override |
| 394 | + public void onProgress(long loaded, long total) { |
| 395 | + callback.onProgress(total > -1, loaded, total); |
| 396 | + } |
| 397 | + }); |
| 398 | + } |
| 399 | + |
363 | 400 | } else if (options.content instanceof JSONObject || options.content instanceof JSONArray) {
|
364 |
| - body = new ProgressRequestBody(RequestBody.create(MediaType.parse(contentType), options.content.toString()), new ProgressListener() { |
365 |
| - @Override |
366 |
| - public void onProgress(long loaded, long total) { |
367 |
| - callback.onProgress(total > -1, loaded, total); |
| 401 | + if (contentType.equals("application/x-www-form-urlencoded")) { |
| 402 | + if (options.content instanceof JSONObject) { |
| 403 | + FormBody.Builder formBody = new FormBody.Builder(StandardCharsets.UTF_8); |
| 404 | + |
| 405 | + for (Iterator<String> it = (((JSONObject) options.content).keys()); it.hasNext(); ) { |
| 406 | + String key = it.next(); |
| 407 | + formBody.addEncoded(key, String.valueOf(((JSONObject) options.content).opt(key))); |
| 408 | + } |
| 409 | + body = new ProgressRequestBody(formBody.build(), new ProgressListener() { |
| 410 | + @Override |
| 411 | + public void onProgress(long loaded, long total) { |
| 412 | + callback.onProgress(total > -1, loaded, total); |
| 413 | + } |
| 414 | + }); |
| 415 | + } else { |
| 416 | + body = new ProgressRequestBody(RequestBody.create(MediaType.parse(contentType), options.content.toString()), new ProgressListener() { |
| 417 | + @Override |
| 418 | + public void onProgress(long loaded, long total) { |
| 419 | + callback.onProgress(total > -1, loaded, total); |
| 420 | + } |
| 421 | + }); |
368 | 422 | }
|
369 |
| - }); |
| 423 | + |
| 424 | + }else { |
| 425 | + body = new ProgressRequestBody(RequestBody.create(MediaType.parse(contentType), options.content.toString()), new ProgressListener() { |
| 426 | + @Override |
| 427 | + public void onProgress(long loaded, long total) { |
| 428 | + callback.onProgress(total > -1, loaded, total); |
| 429 | + } |
| 430 | + }); |
| 431 | + } |
| 432 | + |
370 | 433 | } else {
|
371 | 434 | body = RequestBody.create(null, "");
|
372 | 435 | }
|
@@ -409,14 +472,17 @@ public void onProgress(long loaded, long total) {
|
409 | 472 | }
|
410 | 473 | }
|
411 | 474 | });
|
412 |
| - String contentType = call.request().header("Content-Type"); |
| 475 | + String contentType = response.header("Content-Type"); |
413 | 476 | if (contentType == null) {
|
414 |
| - contentType = call.request().header("content-Type"); |
| 477 | + contentType = response.header("content-type"); |
415 | 478 | }
|
416 | 479 | String acceptHeader;
|
417 | 480 |
|
418 | 481 | if (contentType == null) {
|
419 |
| - acceptHeader = call.request().header("Accept"); |
| 482 | + acceptHeader = response.header("Accept"); |
| 483 | + if (acceptHeader == null){ |
| 484 | + acceptHeader = response.header("accept"); |
| 485 | + } |
420 | 486 | } else {
|
421 | 487 | acceptHeader = contentType;
|
422 | 488 | }
|
|
0 commit comments