• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

Make a multi-part POST?

Hi,

I'm trying to use the following method to POST some data. How would I modify this to also append an image to the POST? I basically need this to be a mutli-part request, and I need to send a few form elements (just some text), then an image.

Thanks
public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}

Open in new window

0
DJ_AM_Juicebox
Asked:
DJ_AM_Juicebox
  • 3
  • 3
1 Solution
 
CSecurityCommented:
See this:
http://www.anddev.org/viewtopic.php?p=14060

you need to do that using MultipartEntity and add as more as parameters and files you want, then using HTTPClient execute it and send MultipartEntity data
0
 
CSecurityCommented:
0
 
DJ_AM_JuiceboxAuthor Commented:
Hi CSecurity,

It looks like that could work, but then I need to import these additional jars to my project - is there any way that I could just open an output stream with an instance of HttpClient, then write the request to the stream myself?

I was previously using HttpURLConnection and doing that, but unfortunately on the platform I'm using, there's an awful but with that and ssl connections which is preventing me from using it. So I need to use HttpClient instead. If I could just open an output stream I should be set.

Thanks
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
CSecurityCommented:
0
 
DJ_AM_JuiceboxAuthor Commented:
Hi CSecurity,

HttpURLConnection is what I was previously using - but the platform I'm on has a bug in it related to ssl stuff - supposedly the platform works ok with HttpClient and ssl - so I have to use that - I'm just wondering if there's a way I can use HttpClient and write the output stream manually (like in the HttpURLConnection example),

Thanks
0
 
cmalakarCommented:
I think, you are trying to use http client 4.x version.. in which MultiPart stuff is not yet ported.

Httpclient 3.x has the support for multipart .. You can try to use, that if that suffices you.

>> if there's a way I can use HttpClient and write the output stream manually

This may possible by extending the HttpClient classes.. but it will very huge work. I never tried like this.
0
 
DJ_AM_JuiceboxAuthor Commented:
ok thanks all, looks like I have to use that multipart entity class, no way around it.

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now