FYI: using extension methods with Java's HttpURLConnection

 From <http://is.gd/nyWI5M>:

> Markus developed workaround which allows making requests with any method, but its dependent on current sun's HttpURLConnection implementation; We don't have much choice on solving this issue in different way, so I'm going to commit it into the trunk BUT users would have to enable some property (not sure yet about its name). I will update this after commit.
>
> proposed workaround:
>
> /**
>   * Workaround for a bug in <code>HttpURLConnection.setRequestMethod(String)</code>: The implementation of Sun Microsystems is throwing a
>   * <code>ProtocolException</code> when the method is other than the HTTP/1.1 default methods. So to use PROPFIND and others, we must apply this workaround.
>   */
>
> private static final void setRequestMethodUsingWorkaroundForJREBug(final HttpURLConnection httpURLConnection, final String method) {
>     try {
>         httpURLConnection.setRequestMethod(method); // Check whether we are running on a buggy JRE
>     } catch (final ProtocolException pe) {
>         try {
>             final Class<?> httpURLConnectionClass = httpURLConnection.getClass();
>             final Field methodField = httpURLConnectionClass.getSuperclass().getDeclaredField("method");
>             methodField.setAccessible(true);
>             methodField.set(httpURLConnection, method);
>         } catch (final Exception e) {
>             throw new RuntimeException(e);
>         }
>     }
> }

Received on Friday, 4 February 2011 13:13:43 UTC