Created
November 7, 2012 01:41
-
-
Save tophyr/4029028 to your computer and use it in GitHub Desktop.
hack to get around HttpURLConnection deficiency
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OutputStream os = null; | |
if (call.getRequestType() == Call.RequestType.DELETE) { | |
try { | |
Object httpImpl = conn; | |
Class<?> httpImplClass = httpImpl.getClass(); | |
if (conn instanceof HttpsURLConnection) { | |
Field delegateField = conn.getClass().getDeclaredField("delegate"); | |
delegateField.setAccessible(true); | |
httpImpl = delegateField.get(conn); | |
httpImplClass = httpImpl.getClass().getSuperclass(); | |
} | |
Field engineField = httpImplClass.getDeclaredField("httpEngine"); | |
Field requestMethodField = HttpURLConnection.class.getDeclaredField("method"); | |
Field rawRequestHeadersField = httpImplClass.getDeclaredField("rawRequestHeaders"); | |
engineField.setAccessible(true); | |
requestMethodField.setAccessible(true); | |
rawRequestHeadersField.setAccessible(true); | |
for (Method method : httpImpl.getClass().getDeclaredMethods()) { | |
if (method.getName().contentEquals("newHttpEngine")) { | |
method.setAccessible(true); | |
Object engine = method.invoke(httpImpl, requestMethodField.get(httpImpl), | |
rawRequestHeadersField.get(httpImpl), | |
null, | |
null); | |
engineField.set(httpImpl, engine); | |
Class<?> httpEngineClass = engine.getClass(); | |
if (conn instanceof HttpsURLConnection) | |
httpEngineClass = httpEngineClass.getSuperclass(); | |
conn.connect(); | |
Method initRequestBodyOut = httpEngineClass.getDeclaredMethod("initRequestBodyOut"); | |
initRequestBodyOut.setAccessible(true); | |
initRequestBodyOut.invoke(engine); | |
Method getRequestBody = httpEngineClass.getDeclaredMethod("getRequestBody"); | |
os = (OutputStream)getRequestBody.invoke(engine); | |
} | |
} | |
} catch (Exception ignored) { } | |
} | |
if (os == null) | |
os = conn.getOutputStream(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment