Skip to content

Instantly share code, notes, and snippets.

@naturalett
Created March 17, 2020 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naturalett/4650ec622efa9213dae61ec7e8f22cbc to your computer and use it in GitHub Desktop.
Save naturalett/4650ec622efa9213dae61ec7e8f22cbc to your computer and use it in GitHub Desktop.
setRequestMethod
// Custom HTTP request method
def setRequestMethod(HttpURLConnection c, String requestMethod) {
try {
final Object target;
if (c instanceof HttpsURLConnectionImpl) {
final Field delegate = HttpsURLConnectionImpl.class.getDeclaredField("delegate");
delegate.setAccessible(true);
target = delegate.get(c);
} else {
target = c;
}
final Field f = HttpURLConnection.class.getDeclaredField("method");
f.setAccessible(true);
f.set(target, requestMethod);
} catch (IllegalAccessException | NoSuchFieldException ex) {
throw new AssertionError(ex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment