hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Balaji Dakshinamurthy" <dbal...@hotmail.com>
Subject Sending a SOAP request with Attachment thru PostMethod
Date Tue, 10 Jul 2007 20:43:06 GMT
  I am a newbie to HttpClient package. I would really appreciate any help 
with the issue that i am facing.

I am trying to send a SOAP request with one attachment (XML ) to a web 
service (SOAP over HTTP) through the PostMethod. I was not able to find any 
documentation on the steps to follow to send a SOAP request with attachment 
(XML) .

Here are my questions:
1. Do i have to use MultipartRequestEntity? Bcos i saw a post in this 
mailing list in May 2007 from a user who tried to do it this way? I was not 
able to find out if he was successful.

2. For this type of submission, does it have to Multipart HTTP request?

2. The way i have implemented is :
  * Formulate the SOAPMessage using SAAJ API.
  * Create the attachment part and attach it to the SOAPMessage.
  * Create a PostMethod and set the RequestEntity as a 
ByteArrayRequestEntity by including the SOAPMessage's contents (using 
SOAPMessage.writeTo(.) method)
  * Set the PostMethod's contentType as "text/xml"

Is this is right way to do it?

I have included the code and the snippet of WSDL.

	PostMethod method = new PostMethod(URL);

	SOAPMessage msg = MessageFactory.newInstance().createMessage();
	SOAPPart sp = msg.getSOAPPart();
	SOAPEnvelope envelope = sp.getEnvelope();
	SOAPBody bdy = envelope.getBody();

	// Formulate SOAPMessage
	SOAPBodyElement be = bdy.addBodyElement(envelope.createName("Submission"));
	msg.getMimeHeaders().addHeader("SOAPAction", "https://ttt.com/Submission");

	// TEST_Agency.txt is file with XML document that i want to
	// send as a SOAP Attachment.
	InputStream att = getClass().getResourceAsStream("TEST_Agency.txt");
	byte[] bytes = StreamUtil.readBytes(att);
	DataHandler dh = new DataHandler(new ByteArrayDataSource("submission", 
"application/octet-stream", bytes));
	AttachmentPart ap = msg.createAttachmentPart(dh);

	ByteArrayOutputStream stream = new ByteArrayOutputStream();
	String string = new String(stream.toByteArray());

	HttpClient client = new HttpClient();
	MimeHeaders mimeHeaders = msg.getMimeHeaders();
	Iterator it = mimeHeaders.getAllHeaders();
	String contType = "";
	while (it.hasNext()) {
		MimeHeader header = (MimeHeader)it.next();
		contType = header.getValue();
		method.setRequestHeader(header.getName(), contType);

	method.setRequestHeader("Content-Type", "text/xml");
	ByteArrayRequestEntity byteReqEnt = new 
ByteArrayRequestEntity(stream.toByteArray(), "text/xml");
	int status = client.executeMethod(method);

WSDL Snippet:
<operation name="Submission">
   <soap:operation soapAction="http...." style="document"/>
             <soap:body parts="Header" use="literal"/>
             <mime:content part="Data" type="application/octet-stream"/>
         <soap:body use="literal"/>


To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org

View raw message