Tuesday, December 30, 2014

Problems with SOAP header








I am trying to create a Swing app that connects to a SOAP API. I have never messed around with web services so I sure this is user error :) The code below throws an error that says "Server did not recognize the value of HTTP Header SOAPAction". Any help would be appreciated.












Java Code:











import java.io.IOException;
import javax.swing.JOptionPane;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

public class KeyMaker {

public void getkey(String certificate) {

String serverURI = "https://ecomapi.networksolutions.com/soapservice.asmx?wsdl";

// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory;
try {
//create a SOAP connection
soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();

// Send SOAP Message to SOAP Server
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(serverURI), serverURI);

// print SOAP Response
System.out.print("Response SOAP Message:");
soapResponse.writeTo(System.out);
soapConnection.close();

} catch (SOAPException | UnsupportedOperationException ex) {
JOptionPane.showMessageDialog(null,
"Something is wrong with the SOAP API: " + ex, "SOAP API Error", JOptionPane.ERROR_MESSAGE);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null,
"Something is wrong with the SOAP API: " + ex, "SOAP API Error", JOptionPane.ERROR_MESSAGE);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null,
"Something is wrong with the SOAP API: " + ex, "SOAP API Error", JOptionPane.ERROR_MESSAGE);
}
}

private SOAPMessage createSOAPRequest(String serverURI) throws Exception {

MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();

// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("urn", serverURI);

/*
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:networksolutions:apis">
<soapenv:Header>
<urn:SecurityCredential>
<urn:Application>MyApplication</urn:Application>
<urn:Certificate>e5fd81293084376d9b326012e483</urn:Certificate>
</urn:SecurityCredential>
</soapenv:Header>
<soapenv:Body>
<urn:GetUserKeyRequest/>
</soapenv:Body>
</soapenv:Envelope>
*/

SOAPBody sb = envelope.getBody();
SOAPElement soapBodyElem = sb.addChildElement("GetUserKeyRequest", "urn");

MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI);
headers.addHeader("urn", "SecurityCredential");
headers.addHeader("urn:Application", "Inventory Adjuster");
headers.addHeader("urn:Certificate", "c8678798982726367281919176632");

soapMessage.saveChanges();

/* Print the request message */
System.out.print("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println();

return soapMessage;
}
}
















No comments:

Post a Comment