diff --git a/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnector.java b/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnector.java index 19050dd835..e88a3802f3 100644 --- a/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnector.java +++ b/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnector.java @@ -355,15 +355,57 @@ private static void setRequestMethodViaJreBugWorkaround(final HttpURLConnection } catch (final ProtocolException pe) { try { final Class httpURLConnectionClass = httpURLConnection.getClass(); - AccessController.doPrivileged(new PrivilegedExceptionAction() { - @Override - public Object run() throws NoSuchFieldException, IllegalAccessException { - final Field methodField = httpURLConnectionClass.getSuperclass().getDeclaredField("method"); - methodField.setAccessible(true); - methodField.set(httpURLConnection, method); - return null; - } - }); + AccessController + .doPrivileged(new PrivilegedExceptionAction() { + @Override + public Object run() throws NoSuchFieldException, + IllegalAccessException { + try { + httpURLConnection.setRequestMethod(method); + // Check whether we are running on a buggy + // JRE + } catch (final ProtocolException pe) { + Class connectionClass = httpURLConnection + .getClass(); + Field delegateField = null; + try { + delegateField = connectionClass + .getDeclaredField("delegate"); + delegateField.setAccessible(true); + HttpURLConnection delegateConnection = (HttpURLConnection) delegateField + .get(httpURLConnection); + setRequestMethodViaJreBugWorkaround( + delegateConnection, method); + } catch (NoSuchFieldException e) { + // Ignore for now, keep going + } catch (IllegalArgumentException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + try { + Field methodField; + while (connectionClass != null) { + try { + methodField = connectionClass + .getDeclaredField("method"); + } catch (NoSuchFieldException e) { + connectionClass = connectionClass + .getSuperclass(); + continue; + } + methodField.setAccessible(true); + methodField.set(httpURLConnection, + method); + break; + } + } catch (final Exception e) { + throw new RuntimeException(e); + } + } + return null; + } + }); } catch (final PrivilegedActionException e) { final Throwable cause = e.getCause(); if (cause instanceof RuntimeException) {