?
Solved

Make a multi-part POST?

Posted on 2009-12-19
7
Medium Priority
?
246 Views
Last Modified: 2012-05-08
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
Comment
Question by:DJ_AM_Juicebox
  • 3
  • 3
7 Comments
 
LVL 17

Accepted Solution

by:
CSecurity earned 2000 total points
ID: 26089455
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
 
LVL 17

Expert Comment

by:CSecurity
ID: 26089460
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 26089481
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:CSecurity
ID: 26089574
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 26089589
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
 
LVL 23

Expert Comment

by:cmalakar
ID: 26089596
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
 

Author Comment

by:DJ_AM_Juicebox
ID: 26091558
ok thanks all, looks like I have to use that multipart entity class, no way around it.

Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question