Skip to content

Instantly share code, notes, and snippets.

@helgew
Created April 26, 2012 20:18
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 helgew/2502765 to your computer and use it in GitHub Desktop.
Save helgew/2502765 to your computer and use it in GitHub Desktop.
public static Serializable doPost(String url, Serializable object) throws Exception {
String contentType = HttpInvokerService.REMOTE_INVOCATION_CONTENT_TYPE + ";charset=" + ENCODING;
HttpTester request = getInstance().getRequest(url);
request.setMethod("POST");
request.setHeader("Content-Type", contentType);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(object);
oos.close();
byte[] oBytes = baos.toByteArray();
request.setContent(new String(oBytes, ENCODING));
String rawRequest = request.generate();
int i = rawRequest.indexOf("\r\n\r\n");
rawRequest = rawRequest.substring(0, i+4);
byte[] allBytes = ArrayUtils.addAll(rawRequest.getBytes(ENCODING), oBytes);
ByteArrayBuffer responseBuffer = tester.getResponses(new ByteArrayBuffer(allBytes));
HttpTester response = new HttpTester();
response.parse(responseBuffer.toString());
parseCookies(response);
byte[] array = responseBuffer.asArray();
int offset = 0;
for (i = 0; i < array.length; i++) {
// Start of content ("\r\n\r\n")
if (array[i] == 13 && array[i + 1] == 10 && array[i + 2] == 13 && array[i + 3] == 10) {
offset = i + 4;
break;
}
}
byte[] range = Arrays.copyOfRange(array, offset, array.length);
return (Serializable) new ObjectInputStream(new ByteArrayInputStream(range)).readObject();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment