Hi,
I'm using the SAAJ API to send a message with an attachment to a .Net web service. I followed the example in the J2EE tutorial and my code manages to call the web service correctly when I don't include an attachment, however when I include an attachment it returns the following error:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><wsu:Timestamp xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility"><wsu:Created>2005-06-01T19:43:39Z</wsu:Created><wsu:Expires>2005-06-01T19:48:39Z</wsu:Expires></wsu:Timestamp></soap:Header><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>System.Web.Services.Protocols.SoapHeaderException: The message is not a valid XML message ---> System.Xml.XmlException: The data at the root level is invalid. Line 1, position 1.
at System.Xml.XmlTextReader.ParseRoot()
at System.Xml.XmlTextReader.Read()
at Microsoft.Web.Services.XmlSkipDTDReader.Read()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at Microsoft.Web.Services.SoapEnvelope.Load(Stream stream)
...
Here is the code:
// Prepare the message
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
MimeHeaders hd = message.getMimeHeaders();
hd.addHeader("SOAPAction", "http://tempuri.org/EchoString");
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope env = soapPart.getEnvelope();
SOAPBody body = message.getSOAPBody();
SOAPFactory soapFactory = SOAPFactory.newInstance();
Name bodyName = soapFactory.createName( "EchoString", "", "http://tempuri.org/" );
SOAPBodyElement bodyElement = body.addBodyElement( bodyName );
Name name = soapFactory.createName("param");
SOAPElement param = bodyElement.addChildElement( name );
param.addTextNode( "ParamValue");
AttachmentPart ap1 = message.createAttachmentPart("Attachment", "text/plain");
message.addAttachmentPart( ap1 );
// Send the message
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
URL endPoint = new URL( "http://localhost/WSEcho/Echo.asmx" );
SOAPMessage response = soapConnection.call( message, endPoint );
Anybody seen this error?
Thanks in advance.
Craig