1

我的肥皂连接代码:

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);  
4

1 回答 1

5

您需要将代码更改为这样-

   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);

这将解决您的问题。

于 2014-02-02T17:16:46.517 回答