我试图从WebSphere 6.1中调用SOAP Web服务。我可以使用Apache Tomcat 6.0.36运行时运行代码。但是,使用WebSphere 6.1,我收到以下错误:
Jan 5, 2015 7:19:23 PM com.ibm.ws.ssl.config.SSLConfigManager INFO:ssl.disable.url.hostname.verification.CWPKI0027I
Jan 5, 2015 7:19:24 PM com.ibm.ws.channel.framework.impl.WSChannelFrameworkImpl AUDIT: chain.started
Jan 5, 2015 7:19:25 PM com.ibm.ws.webservices.engine.PivotHandlerWrapper invoke WARNING:
WSWS3734W: Warning: Exception caught from invocation to com.ibm.ws.webservices.engine.transport.http.HTTPSender:
WebServicesFault faultCode:
HTTP faultString: ( 401 ) Unauthorized faultActor: http://server.customer.com:80
faultDetail: null:
WSWS3192E: Error: return code: ( 401 ) Unauthorized
这是我正在运行的代码,它可以在Apache Tomcat中正常运行:
public class Test {
public void submitOrder()
{
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
String url = "http://server.customer.com/serviceEndpoint";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
// Process the SOAP Response
printSOAPResponse(soapResponse);
soapConnection.close();
} catch (Exception e) {
System.err.println("Error occurred while sending SOAP Request to Server");
e.printStackTrace();
}
}
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
MimeHeaders hd = soapMessage.getMimeHeaders();
String username = "xxxxx";
String password = "xxxxx";
byte [] auth = (username+":"+password).getBytes();
String authorization = new String ( Base64.encodeBase64(auth) );
System.out.println ( "authorization = " + authorization );
hd.addHeader("Authorization", "Basic " + authorization);
SOAPPart soapPart = soapMessage.getSOAPPart();
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement ordersElem = soapBody.addChildElement("Orders");
// Need to add two attributes to this node!
SOAPElement orderElem = ordersElem.addChildElement("Order");
Name codeAttributeName = envelope.createName("code");
orderElem.addAttribute(codeAttributeName, "00001");
Name timeAttributeName = envelope.createName("time");
orderElem.addAttribute(timeAttributeName, "2015-01-04 12:00:00 PM");
SOAPElement salesOrderConfElem = orderElem.addChildElement("SalesOrderConfirmationCode");
salesOrderConfElem.addTextNode("16041");
SOAPElement statusElem = orderElem.addChildElement("Status");
Name statusCodeAttributeName = envelope.createName("code");
statusElem.addAttribute(statusCodeAttributeName, "COMPLETE");
SOAPElement entriesElem = orderElem.addChildElement("Entries");
SOAPElement entryElem = entriesElem.addChildElement("Entry");
SOAPElement entryNumElem = entryElem.addChildElement("EntryNumber");
entryNumElem.addTextNode("1");
SOAPElement productElem = entryElem.addChildElement("ProductCode");
productElem.addTextNode("738053571");
SOAPElement qtyElem = entryElem.addChildElement("Quantity");
qtyElem.addTextNode("1");
SOAPElement shippedElem = entryElem.addChildElement("Shipped");
shippedElem.addTextNode("1");
SOAPElement backOrderElem = entryElem.addChildElement("Backordered");
backOrderElem.addTextNode("1");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", "http://sap.com/xi/WebService/soap1.1");
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
/**
* Method used to print the SOAP Response
*/
private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.print("\nResponse SOAP Message = ");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
}
/**
* @param args
*/
public static void main(String[] args) {
Test t = new Test();
t.submitOrder();
}
}
更正:Tomcat和WebSphere创建的SOAP信封不一样。 Tomcat使用Java 1.6创建以下内容:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<Orders>
而WebSphere,使用IBM的1.5版本,创建了以下内容:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<Orders>
我如何让WebSphere创建与Tomcat相同的SOAP信封?