You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Matthieu Labour <ma...@kikin.com> on 2009/06/04 23:42:26 UTC
Sending binary data using HttpPost
Hi
I apologize if this question is basic
I want to send binary data using the http post method of the HttpClient
library. Unfortunately the addParameter allows me to set only Strings
How can I achieve the following using HttpClient? Thank you for your help!
public static byte[] compress(byte[] input) {
try {
final ByteArrayOutputStream targetStream = new
ByteArrayOutputStream(input.length);
OutputStream outputStream = new
java.util.zip.GZIPOutputStream(targetStream);
outputStream.write(input);
outputStream.close();
return targetStream.toByteArray();
} catch (IOException e) {
throw new RuntimeException("impossible exception?", e);
}
}
.....
HttpClient client = new HttpClient
PostMethod httpPost = new PostMethod("
http://localhost:8080/httpclienttest/body");
byte[] dataCompressed = compress(data);
hppPost.set("Body", dataCompressed);
client.executeMethod(httpPost);
Re: Sending binary data using HttpPost
Posted by wilson wong <ws...@yahoo.com>.
PostMethod post = new PostMethod(http://your.post.com);
RequestEntity entity = new ByteArrayRequestEntity(zipbytearray);
post.setRequestEntity(entity);
httpclient.executeMethod(post);
You might need to encoded the zip byte array using Base64
Hope that help
Wilson
----- Original Message ----
From: Matthieu Labour <ma...@kikin.com>
To: httpclient-users@hc.apache.org
Sent: Thursday, June 4, 2009 5:42:26 PM
Subject: Sending binary data using HttpPost
Hi
I apologize if this question is basic
I want to send binary data using the http post method of the HttpClient
library. Unfortunately the addParameter allows me to set only Strings
How can I achieve the following using HttpClient? Thank you for your help!
public static byte[] compress(byte[] input) {
try {
final ByteArrayOutputStream targetStream = new
ByteArrayOutputStream(input.length);
OutputStream outputStream = new
java.util.zip.GZIPOutputStream(targetStream);
outputStream.write(input);
outputStream.close();
return targetStream.toByteArray();
} catch (IOException e) {
throw new RuntimeException("impossible exception?", e);
}
}
.....
HttpClient client = new HttpClient
PostMethod httpPost = new PostMethod("
http://localhost:8080/httpclienttest/body");
byte[] dataCompressed = compress(data);
hppPost.set("Body", dataCompressed);
client.executeMethod(httpPost);
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org