HttpClient post with Content-Disposition

痴心易碎 提交于 2019-12-12 00:27:00

问题


Good evening.

Usually I'm working with post request like thatname1=value1&name2=value2 and my code is

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name1", "value1"));
httppost.setEntity(new UrlEncodedFormEntity("name2","value2");

But now I have the post like that

-----------------------------17911109517875 Content-Disposition: form-data;
name="PERSON*1[F*2][2664]" value1 
-----------------------------17911109517875 Content-Disposition: form-data; 
name="PERSON*1[I*3][2776]" value2 
-----------------------------17911109517875 Content-Disposition: form-data;  
name="PERSON*1[O*4][2778]" value3

So, as far as I know, I should do

nameValuePairs.add(new BasicNameValuePair("PERSON*1[F*2][2664]", "value1"));

But what with content-disposition ?

Thank you.


回答1:


You'll need to utilize HttpClient's HttpMime support. This is not included out of the box with Android so you will have to bundle it with your application.

An example, based on your post could be accomplished as follows:

    MultipartEntity mpe= new MultipartEntity();
    FormBodyPart part1= new FormBodyPart("PERSON*1[F*2][2664]", new StringBody("value1"));
    FormBodyPart part2= new FormBodyPart("PERSON*1[I*3][2776]", new StringBody("value2")); 
    FormBodyPart part3= new FormBodyPart("PERSON*1[O*4][2778]", new StringBody("value3"));
    mpe.addPart(part1);
    mpe.addPart(part2);
    mpe.addPart(part3);

An example of the above output to a stream would be as follows:

--ZV5t1WLAh04TJTqjyBJBSDL3M69xu0A
Content-Disposition: form-data; name="PERSON*1[F*2][2664]"
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 8bit

value1
--ZV5t1WLAh04TJTqjyBJBSDL3M69xu0A
Content-Disposition: form-data; name="PERSON*1[I*3][2776]"
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 8bit

value2
--ZV5t1WLAh04TJTqjyBJBSDL3M69xu0A
Content-Disposition: form-data; name="PERSON*1[O*4][2778]"
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 8bit

value3
--ZV5t1WLAh04TJTqjyBJBSDL3M69xu0A--

I believe the library is more or less stand-alone and can be retrieved from the httpclient website.



来源:https://stackoverflow.com/questions/17320886/httpclient-post-with-content-disposition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!