Wednesday, August 3, 2011

SOAP web service with custom headers - SOAP Action

When calling .net webservices with custom headers don't forget the evilness of SOAPAction.

String operation = "HelloThere"; // "FetchMerchants"; // could
// also be
String destination = "http://localhost/interfaces/reseller/BusinessLayer/ResellerWS.asmx";

// First create the connection
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory
.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();

// Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();

SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("xsd",
"http://www.w3.org/2001/XMLSchema");
envelope.addNamespaceDeclaration("xsi",
"http://www.w3.org/2001/XMLSchema-instance");
// This method demonstrates how to set HTTP and SOAP headers.
// setOptionalHeaders(message, envelope);
envelope.removeNamespaceDeclaration("soapenc");

// Create and populate the body
SOAPBody body = envelope.getBody();

// Create the main element and namespace
SOAPElement bodyElement = body.addChildElement(envelope.createName(
operation, "", "http://tempuri.org/"));
// Add parameters

bodyElement.addChildElement("mid").addTextNode("3");// 41221");

// envelope.getHeader().detachNode();
SOAPHeader header = envelope.getHeader(); // xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"

// QName hName = envelope.createQName("Header", "");
// header.setElementQName(hName);

// Hearder - Authentication info
Name headerElementName = envelope.createName("AuthHeader", "",
"http://tempuri.org/");

SOAPHeaderElement headerElement = header
.addHeaderElement(headerElementName);
// headerElement.setMustUnderstand(false);
// headerElement.addNamespaceDeclaration("soap",
// "http://schemas.xmlsoap.org/soap/envelope/");
SOAPElement sessionId = headerElement.addChildElement("UID");
sessionId.addTextNode("380");

// Username Password

SOAPElement auth = headerElement.addChildElement("Authorization");
auth.addTextNode("Basic " + "UserIndustries:Authnet101");

// remove SOAPAction from HTTP Header
MimeHeaders mimeHeaders = message.getMimeHeaders();
mimeHeaders.addHeader("SOAPAction", "http://tempuri.org/"
+ operation);

// Save the message
message.saveChanges();

// Send the message and get the reply
SOAPMessage reply = connection.call(message, destination);

// remove soapAction from the header

// Retrieve the result - no error checking is done: BAD!
soapPart = reply.getSOAPPart();
envelope = soapPart.getEnvelope();
body = envelope.getBody();

Iterator iter = body.getChildElements();
Node resultOuter = ((Node) iter.next()).getFirstChild();
Node result = resultOuter.getFirstChild();

System.out.println("result : " + result.toString());
} catch (UnsupportedOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SOAPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}