@@ -355,26 +355,57 @@ private static void setRequestMethodViaJreBugWorkaround(final HttpURLConnection
355355 } catch (final ProtocolException pe ) {
356356 try {
357357 final Class <?> httpURLConnectionClass = httpURLConnection .getClass ();
358- AccessController .doPrivileged (new PrivilegedExceptionAction <Object >() {
359- @ Override
360- public Object run () throws NoSuchFieldException , IllegalAccessException {
361- final Class <?> parentClass = httpURLConnectionClass
362- .getSuperclass ();
363- final Field methodField ;
364- // If the implementation class is an HTTPS URL Connection, we
365- // need to go up one level higher in the heirarchy to modify the
366- // 'method' field.
367- if (parentClass == HttpsURLConnection .class ) {
368- methodField = parentClass .getSuperclass ().getDeclaredField (
369- "method" );
370- } else {
371- methodField = parentClass .getDeclaredField ("method" );
372- }
373- methodField .setAccessible (true );
374- methodField .set (httpURLConnection , method );
375- return null ;
376- }
377- });
358+ AccessController
359+ .doPrivileged (new PrivilegedExceptionAction <Object >() {
360+ @ Override
361+ public Object run () throws NoSuchFieldException ,
362+ IllegalAccessException {
363+ try {
364+ httpURLConnection .setRequestMethod (method );
365+ // Check whether we are running on a buggy
366+ // JRE
367+ } catch (final ProtocolException pe ) {
368+ Class <?> connectionClass = httpURLConnection
369+ .getClass ();
370+ Field delegateField = null ;
371+ try {
372+ delegateField = connectionClass
373+ .getDeclaredField ("delegate" );
374+ delegateField .setAccessible (true );
375+ HttpURLConnection delegateConnection = (HttpURLConnection ) delegateField
376+ .get (httpURLConnection );
377+ setRequestMethodViaJreBugWorkaround (
378+ delegateConnection , method );
379+ } catch (NoSuchFieldException e ) {
380+ // Ignore for now, keep going
381+ } catch (IllegalArgumentException e ) {
382+ throw new RuntimeException (e );
383+ } catch (IllegalAccessException e ) {
384+ throw new RuntimeException (e );
385+ }
386+ try {
387+ Field methodField ;
388+ while (connectionClass != null ) {
389+ try {
390+ methodField = connectionClass
391+ .getDeclaredField ("method" );
392+ } catch (NoSuchFieldException e ) {
393+ connectionClass = connectionClass
394+ .getSuperclass ();
395+ continue ;
396+ }
397+ methodField .setAccessible (true );
398+ methodField .set (httpURLConnection ,
399+ method );
400+ break ;
401+ }
402+ } catch (final Exception e ) {
403+ throw new RuntimeException (e );
404+ }
405+ }
406+ return null ;
407+ }
408+ });
378409 } catch (final PrivilegedActionException e ) {
379410 final Throwable cause = e .getCause ();
380411 if (cause instanceof RuntimeException ) {
0 commit comments