我的肥皂连接代码:
MessageFactory msgFactory = MessageFactory.newInstance();
SOAPMessage message = msgFactory.createMessage();
String loginPassword = "user:password";
message.getMimeHeaders().addHeader("Authorization", "Basic " + Base64.encode(loginPassword.getBytes()).toString());
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
String url = "http://servername/name1";
SOAPMessage soapResponse = soapConnection.call(message, url);
我总是得到异常:
CAUSE:
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (401Unauthorized
怎么了?
请求:
String soapText =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+"<soap:Body>"
+"<ReceiveOrder xmlns=\"http://url/server/name\">"
+"<Order xmlns=\"http://url/name\">"
+"<Order_Number>54321</Order_Number>"
+"<Order_Date>2013-07-26</Order_Date>"
+"<Supply_Date>2013-07-27</Supply_Date>"
+"<Supply_Time>12:00</Supply_Time>"
+"<Version>1</Version>"
+"<Autor>Леся</Autor>"
+"<Type>B</Type>"
+"<Supplyer>3032</Supplyer>"
+"<Mag_Number>138</Mag_Number>"
+"<Transaction>54321</Transaction>"
+"<Rows>"
+"<Row>"
+"<Row_Number>1</Row_Number>"
+"<Ware>29</Ware>"
+"<Сount>10</Сount>"
+"<Ware_Name>Банан</Ware_Name>"
+"<GTIN>37268</GTIN>"
+"</Row>"
+"</Rows>"
+"</Order>"
+"</ReceiveOrder>"
+"</soap:Body>"
+"</soap:Envelope>";
SOAPPart soapPart = message.getSOAPPart();
// Load the SOAP text into a stream source
byte[] buffer = soapText.getBytes();
ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
StreamSource source = new StreamSource(stream);
// Set contents of message
soapPart.setContent(source);
最佳答案
您需要更改代码以使其符合以下条件-
MessageFactory msgFactory = MessageFactory.newInstance();
SOAPMessage message = msgFactory.createMessage();
String loginPassword = "user:password";
message.getMimeHeaders().addHeader("Authorization", "Basic " + new String(Base64.encode(loginPassword.getBytes())));
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
String url = "http://servername/name1";
SOAPMessage soapResponse = soapConnection.call(message, url);
这样可以解决您的问题。
关于java - SOAP授权,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17881841/