Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

WebService HttpClient MultipartRequestEntity

v leo
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic