Sending binary data using HttpPost

View: New views
2 Messages — Rating Filter:   Alert me  

Sending binary data using HttpPost

by Matthieu Labour-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

by wilson wong :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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 <matthieu@...>
To: httpclient-users@...
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@...
For additional commands, e-mail: httpclient-users-help@...