-1

i'm trying to simulate a HttpPost request for form submission using HttpClient 4.2.3

the form is similar to

<form action="localhost/xyz.aspx" method = "post" enctype="multipart/form-data">
     <input type="text" name="name">
     <input type="text" name="age">
     <input type="text" name="submit">
</form>

When i tried using java code like,

 List<NameValuePair> formparams1 = new ArrayList<NameValuePair>();
 formparams1.add(new BasicNameValuePair("name","john"));
 formparams1.add(new BasicNameValuePair("age", "10"));
 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams1);
 entity.setContentEncoding("multipart/form-data");
 entity.setContentType("multipart/form-data;boundary=--asd123");

i receive HTTP Status 400 - java.lang.RuntimeException: Could find no Content-Disposition header within part

Also i searched a while and tried another way,

MultipartEntity entity = new MultipartEntity();
entity.addPart("name",new StringBody("john",Charset.forName("UTF-8")));
entity.addPart("age", new StringBody("10",Charset.forName("UTF-8")));

Still i get an error like HTTP 415 HTTP 400

Could anyone pls help me in simulating such a request.

TIA

siva
  • 1,429
  • 3
  • 26
  • 47
  • This question could be helpful to you: http://stackoverflow.com/questions/2304663/apache-httpclient-making-multipart-form-post – Andremoniy Jan 16 '13 at 07:14
  • @Andremoniy i have exactly tried the same and it results in **No Content-Disposition found in part** If possible u can even help on that. TIA – siva Jan 16 '13 at 07:35

1 Answers1

1

The multipart entity that you are sending needs two objects to be set FileBody and StringBody. What you are setting is only StringBody.

In short Multipart requests generally consists of files. The server needs the file name (set using StringBody) and file contents (set using FileBody).

For example.

FileBody name = new FileBody(new File(fileName));
StringBody content = new StringBody("Filename: " + fileName);

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("name", bin);
reqEntity.addPart("content", content);
httppost.setEntity(reqEntity);

In your case, you dont seem to be sending any mutlipart contents. I am not sure by are using that encoding type!

Santosh
  • 17,667
  • 4
  • 54
  • 79
  • i dont have to send any file content. i just reverse engineering jBPM console. so i have to simulate the same request. Could u pls elaborate if i'm wrong. – siva Jan 16 '13 at 07:38
  • Simply remove the multipart related stuff from your code and check if thats working. – Santosh Jan 16 '13 at 07:42
  • @Santhosh i'm actually trying to the same thing. https://community.jboss.org/thread/203492?start=0&tstart=0 – siva Jan 16 '13 at 07:45