users@jersey.java.net

HTTP Form Posting using Jersey Client vs Apache HttpClient

From: Arul Dhesiaseelan <aruld.info_at_gmail.com>
Date: Sun, 11 Apr 2010 21:12:34 -0600

Hi,

I am trying to use Jersey client to do a multipart form submit to a CGI
script. But, the service returns the HTML form instead of the actual
response. I have the code which works on Apache HttpClient. I am not sure
what I am missing here. Can someone help?

POST using Apache HttpClient (this code works):

HttpClient client = new HttpClient();
PostMethod filePost = new PostMethod("https://test.com/cgi-bin/submit.cgi");
filePost.setDoAuthentication(true);//process authentication challenges
authomatically
File targetFile = new File("customer.bin");
Part[] parts = { new FilePart("file", targetFile),
    new StringPart("user", "username"),
    new StringPart("password", "password"),
    new StringPart("type", "X32") };
filePost.setRequestEntity(new MultipartRequestEntity(parts,
filePost.getParams()));
filePost.setRequestHeader("Content-type","multipart/form-data");
int status = client.executeMethod(filePost);

Log output:

2010/04/11 17:20:21:594 MDT [DEBUG] header - >> "POST /cgi-bin/submit.cgi
HTTP/1.1[\r][\n]"
2010/04/11 17:20:21:595 MDT [DEBUG] HttpMethodBase - Adding Host request
header
2010/04/11 17:20:21:620 MDT [DEBUG] header - >> "Content-type:
multipart/form-data[\r][\n]"
2010/04/11 17:20:21:621 MDT [DEBUG] header - >> "User-Agent: Jakarta
Commons-HttpClient/3.1[\r][\n]"
2010/04/11 17:20:21:621 MDT [DEBUG] header - >> "Host: test.com[\r][\n]"
2010/04/11 17:20:21:621 MDT [DEBUG] header - >> "Content-Length:
117417[\r][\n]"
2010/04/11 17:20:21:621 MDT [DEBUG] header - >> "[\r][\n]"
2010/04/11 17:20:21:622 MDT [DEBUG] content - >> "--"
2010/04/11 17:20:21:622 MDT [DEBUG] content - >>
"giTfSy0R4Zrd7j4lruBwymHkrZRJdNLD5kV4zcM"
2010/04/11 17:20:21:622 MDT [DEBUG] content - >> "[\r][\n]"
2010/04/11 17:20:21:622 MDT [DEBUG] content - >> "Content-Disposition:
form-data; name="
2010/04/11 17:20:21:623 MDT [DEBUG] content - >> """
2010/04/11 17:20:21:623 MDT [DEBUG] content - >> "file"
2010/04/11 17:20:21:623 MDT [DEBUG] content - >> """
2010/04/11 17:20:21:623 MDT [DEBUG] content - >> "; filename="
2010/04/11 17:20:21:623 MDT [DEBUG] content - >> """
2010/04/11 17:20:21:623 MDT [DEBUG] content - >> "customer.bin"
2010/04/11 17:20:21:623 MDT [DEBUG] content - >> """
2010/04/11 17:20:21:623 MDT [DEBUG] content - >> "[\r][\n]"
2010/04/11 17:20:21:623 MDT [DEBUG] content - >> "Content-Type: "
2010/04/11 17:20:21:623 MDT [DEBUG] content - >> "application/octet-stream"
2010/04/11 17:20:21:624 MDT [DEBUG] content - >> "; charset="
2010/04/11 17:20:21:624 MDT [DEBUG] content - >> "ISO-8859-1"
2010/04/11 17:20:21:624 MDT [DEBUG] content - >> "[\r][\n]"
2010/04/11 17:20:21:624 MDT [DEBUG] content - >> "Content-Transfer-Encoding:
"
2010/04/11 17:20:21:624 MDT [DEBUG] content - >> "binary"
2010/04/11 17:20:21:624 MDT [DEBUG] content - >> "[\r][\n]"
2010/04/11 17:20:21:624 MDT [DEBUG] content - >> "[\r][\n]"
...............

POST using Jersey Client (this does not work) :

MultiPart multiPart = new MultiPart();
WebResource.Builder builder = resource.getRequestBuilder();
File targetFile = new File("customer.bin");
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file", targetFile,
MediaType.APPLICATION_OCTET_STREAM_TYPE);
multiPart.bodyPart(fileDataBodyPart);
FormDataBodyPart formDataBodyPart = new FormDataBodyPart("user", "username);
multiPart.bodyPart(formDataBodyPart);
formDataBodyPart = new FormDataBodyPart("password", "password");
multiPart.bodyPart(formDataBodyPart);
formDataBodyPart = new FormDataBodyPart("tyoe", "X32");
multiPart.bodyPart(formDataBodyPart);
ClientResponse response =
builder.type("multipart/form-data").post(ClientResponse.class, multiPart);

Log output:

INFO: 1 * Client out-bound request
1 > POST https://test.com/cgi-bin/submit.cgi
1 > Content-Type: multipart/form-data
1 >

--Boundary_1_1839257702_1271040995070
Content-Type: application/octet-stream
Content-Disposition: form-data;name="file"

........

I tried using FormDataMultiPart, but no luck. I am not sure what translates
to setDoAuthentication() API in Jersey Client.

I also tried something like using the ApacheHttpClient API in Jersey Client,
but same result:

        DefaultApacheHttpClientConfig config = new
DefaultApacheHttpClientConfig();
        config.getState().setCredentials(null, null, -1, "username",
"password");

config.getProperties().put(ApacheHttpClientConfig.PROPERTY_PREEMPTIVE_AUTHENTICATION,
true);

        ApacheHttpClient client = ApacheHttpClient.create(config);

I must be missing something fundamental here. Appreciate any suggestions.

-Arul