@@ -391,15 +391,57 @@ private static void setRequestMethodViaJreBugWorkaround(final HttpURLConnection
391391 } catch (final ProtocolException pe ) {
392392 try {
393393 final Class <?> httpURLConnectionClass = httpURLConnection .getClass ();
394- AccessController .doPrivileged (new PrivilegedExceptionAction <Object >() {
395- @ Override
396- public Object run () throws NoSuchFieldException , IllegalAccessException {
397- final Field methodField = httpURLConnectionClass .getSuperclass ().getDeclaredField ("method" );
398- methodField .setAccessible (true );
399- methodField .set (httpURLConnection , method );
400- return null ;
401- }
402- });
394+ AccessController
395+ .doPrivileged (new PrivilegedExceptionAction <Object >() {
396+ @ Override
397+ public Object run () throws NoSuchFieldException ,
398+ IllegalAccessException {
399+ try {
400+ httpURLConnection .setRequestMethod (method );
401+ // Check whether we are running on a buggy
402+ // JRE
403+ } catch (final ProtocolException pe ) {
404+ Class <?> connectionClass = httpURLConnection
405+ .getClass ();
406+ Field delegateField = null ;
407+ try {
408+ delegateField = connectionClass
409+ .getDeclaredField ("delegate" );
410+ delegateField .setAccessible (true );
411+ HttpURLConnection delegateConnection = (HttpURLConnection ) delegateField
412+ .get (httpURLConnection );
413+ setRequestMethodViaJreBugWorkaround (
414+ delegateConnection , method );
415+ } catch (NoSuchFieldException e ) {
416+ // Ignore for now, keep going
417+ } catch (IllegalArgumentException e ) {
418+ throw new RuntimeException (e );
419+ } catch (IllegalAccessException e ) {
420+ throw new RuntimeException (e );
421+ }
422+ try {
423+ Field methodField ;
424+ while (connectionClass != null ) {
425+ try {
426+ methodField = connectionClass
427+ .getDeclaredField ("method" );
428+ } catch (NoSuchFieldException e ) {
429+ connectionClass = connectionClass
430+ .getSuperclass ();
431+ continue ;
432+ }
433+ methodField .setAccessible (true );
434+ methodField .set (httpURLConnection ,
435+ method );
436+ break ;
437+ }
438+ } catch (final Exception e ) {
439+ throw new RuntimeException (e );
440+ }
441+ }
442+ return null ;
443+ }
444+ });
403445 } catch (final PrivilegedActionException e ) {
404446 final Throwable cause = e .getCause ();
405447 if (cause instanceof RuntimeException ) {
0 commit comments