0

I needed to send some XML to a webservices and I was able to do it with a normal StringEntity because it was just text but now I need to attach an image to it as well. I tried doing it with a MultipartEntity but I couldn't get it working with just the XML.

// Working

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost doc = new HttpPost("http://mywebservices.com");

HttpEntity entity = new StringEntity(writer.toString());
httppost.setEntity(entity);

HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();

// not working

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost doc = new HttpPost("http://mywebservices.com");

// no difference when removing the BROWSER_COMPATIBLE   
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
entity.addPart("xml", new StringBody(writer.toString()));
httppost.setEntity(entity);

HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();

And is there a way I could see the MIME that is being send?

Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
Mats Hofman
  • 7,060
  • 6
  • 33
  • 48

3 Answers3

4

You simply forgot:

httppost.setEntity(entity);

By the way, it's probably good form to set the Content-Type of your parts, e.g.:

entity.addPart("xml", new StringBody(writer.toString(),"application/xml",Charset.forName("UTF-8")));

As far as seeing what's being sent, see http://hc.apache.org/httpcomponents-client-ga/logging.html (especially "wire logging") for the HttpClient logging features, and this question for how to get it working on Android.

Community
  • 1
  • 1
Robert Tupelo-Schneck
  • 10,047
  • 4
  • 47
  • 58
  • oh I see I forgot to copy the `httppost.setEntity(entity);`, it was in my code but didn't work :( – Mats Hofman Mar 29 '11 at 17:34
  • The webservices is unable to read the request and gives me an error back. So the `MultipartEntity` generates another post than the normal `HttpEntity` but I'm unable to see the post. – Mats Hofman Mar 31 '11 at 07:55
  • Well, the post with the multipart entity will definitely be different from the post with an xml entity. Do you know that the web service accepts multipart? To see the post, use the wire logging from this answer, or see my other answer. – Robert Tupelo-Schneck Mar 31 '11 at 14:05
  • I'm so stupid... I commented the `httppost.setEntity(entity);` line. Thanks a lot for the help! – Mats Hofman Apr 02 '11 at 15:12
1

Another way to see what's being sent is to set up your own "server" to receive the request. You can do this on a Unix-like system with netcat. The command-line

nc -l 1234

starts a server listening on port 1234, and will echo whatever is received.

If that "server" is on a machine 10.1.2.3, you can just use a new HttpPost("http://10.1.2.3:1234") to send the message there.

Robert Tupelo-Schneck
  • 10,047
  • 4
  • 47
  • 58
  • A little addition, if you want to do this on Windows you can download NetCat here http://www.securityfocus.com/tools/139 and you should run it like this. `C:\path\to\nc.exe -l -p 1234` – Mats Hofman Aug 02 '11 at 08:53
0

I have a similar problem but I'm sending the multipart with user/pass to a acegi security system, it works with this:

request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

But not with this:

try {   
    for (NameValuePair param : params) {
        multientity.addPart(param.getName(), new StringBody(param.getValue(),      Charset.forName(encoding))));
    }
    request.setEntity(multientity);
}
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
Maxrunner
  • 1,955
  • 4
  • 24
  • 41