File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Services and the fly likes WebService HttpClient MultipartRequestEntity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "WebService HttpClient MultipartRequestEntity" Watch "WebService HttpClient MultipartRequestEntity" New topic

WebService HttpClient MultipartRequestEntity

v leo

Joined: Jan 31, 2011
Posts: 1
Hello everyone,
I have a problem with HttpClient to call a Web Service.
The call has a structure in which there is an xml tag "<file> </file> ".
The tag file contains the base64 encoding of the file to be sent and the file can be very large. If I encode the file first and then insert the tag, I have a memory problem. So I decided to use the MultipartRequestEntity in the call to web service, but the structure does not match the one that accepts the web service and I have a generic error (httpStatus 500).
How do I populate the tag "<file> </file> " with the contents of the file???


Now I have this structure:


String mimetype = getMimetype(file.getPath());
PostMethod post = new PostMethod(url);

try {
String tempPid = generatePID(new Integer(id));
String soapAction = generateSoapMessageAddDocument(dto, tempPid); //xml xml structure
if (log.isDebugEnabled()) {
log.debug("soapAction is " + soapAction);

MultipartRequestEntity entity = new MultiPartRelatedRequestEntityCogito4(new Part[] {
new SoapCogitoPart(soapAction, tempPid), //extends PartBase
new FileCogitoPart(file.getName(), file, tempPid, mimetype) }, post.getParams()); // extends FilePart


int result = httpclient.executeMethod(post);
log.debug("Response status code: " + result);
if (result == HttpStatus.SC_OK) {
String replyXML = post.getResponseBodyAsString();
if (log.isDebugEnabled()) {
log.debug("xmlResp is " + replyXML);
String response = decodeXml(replyXML);
return response;
} else {
if (log.isDebugEnabled()) {
log.debug("Method failed: " + post.getStatusLine());
log.debug("Response body: " + new String(post.getResponseBody()));

} catch (HttpException e) {
log.debug("Fatal protocol violation: " + e.getMessage());
throw new SystemException(e);
} catch (IOException e) {
log.debug("Fatal transport error: " + e.getMessage());
throw new SystemException(e);
} catch (Throwable e) {
// TODO Auto-generated catch block
} finally {
// Release current connection to the connection pool once you are done
throw new SystemException("Errore http status = " + post.getStatusCode() + " - "
+ post.getStatusText());

I hope I was clear.
I agree. Here's the link:
subject: WebService HttpClient MultipartRequestEntity