hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Richard Jones <rich...@symplectic.co.uk>
Subject Re: mime-boundary headers
Date Thu, 23 Apr 2009 14:15:35 GMT
Hi Oleg,
>>> http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/httpmime/src/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java
>> Following this example, I have re-written my code as follows:
>> HttpClient client = new DefaultHttpClient();
>> HttpPost post = new HttpPost(target);
>> // set the request headers
>> post.setHeader("Slug", slug);
>> post.setHeader("X-On-Behalf-Of", user);
>> // construct the xml and binary file parts
>> FileBody xmlBody = new FileBody(new File(xml));
>> FileBody binBody = new FileBody(new File(binary));
>> MultipartEntity reqEntity = new MultipartEntity();
>> reqEntity.addPart("atom", xmlBody);
>> reqEntity.addPart("binary", binBody);
>> post.setEntity(reqEntity);
>> HttpResponse response = client.execute(post);
>> But I still don't see any way of passing in http headers for the mime  
>> parts.  I presume that I'm missing something fundamental about how to  
>> use httpclient 4 with the httpmime extension, but I can't find any  
>> examples of doing what it is that i'm trying to do to follow.
>> What am I missing?
> You will have to subclass FormBodyPart class and override its
> #generateContentDisp() method. You will also have to implement a custom
> HttpEntity similar to MultipartEntity shipped with HttpMime, which makes use of
> the extended FormBodyPart class.

OK, I think I'm part way there.  After taking a look at the source for 
FormBodyPart, I came up with this, which I think means that I don't 
actually have to extend that class:

FileBody binBody = new FileBody(new File(binary));
FormBodyPart fb = new FormBodyPart("binary", binBody);
fb.getHeader().addField(new MinimalField(MIME.CONTENT_DISPOSITION, 

Now all I need to do is get the FormBodyPart into the request, which is 
where I'm now stuck.  Obviously, the MultipartEntity doesn't take the 
FormBodyPart as an argument to addPart(), although it sounds like it 
should.  Is this what you are suggesting I need to implement for 
HttpEntity, above?  Or is there some other way of getting this 
formbodypart into the request?

If I have some feedback for the httpclient 4.0 version, I'd say that 
this is a particularly challenging and circuitous bit of coding to 
achieve a relatively minor actual change in the http request.  An API to 
do this sort of thing, without needing custom implementations of http 
client interfaces would be really useful.



To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org

View raw message