Hi,
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.
Code:
------
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"));
be.addChildElement("agencyid").addTextNode("81155");
msg.getMimeHeaders().addHeader("SOAPAction",
"https://ttt.com/Submission");
msg.saveChanges();
// 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);
ap.setContentId("SubmissionData");
msg.addAttachmentPart(ap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
msg.writeTo(stream);
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");
method.setRequestEntity(byteReqEnt);
int status = client.executeMethod(method);
WSDL Snippet:
-------------------
<operation name="Submission">
<soap:operation soapAction="http...." style="document"/>
<input>
<mime:multipartRelated>
<mime:part>
<soap:body parts="Header" use="literal"/>
</mime:part>
<mime:part>
<mime:content part="Data" type="application/octet-stream"/>
</mime:part>
</mime:multipartRelated>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
_________________________________________________________________
http://liveearth.msn.com
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]