SOAP授权

时间:2013-07-26 12:54:05

标签: java soap

我的肥皂连接代码:

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

1 个答案:

答案 0 :(得分:4)

您需要将代码更改为此类 -

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

这将解决您的问题。