2013-07-26 96 views
1

我的香皂連接代碼: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); 
+0

如果請求已包含授權憑證,則401響應表明授權已被拒絕。 – Veera

+0

當我通過瀏覽器打開我的網址:http:// servername/name1它問我的憑據,證書的作品。 –

+0

您是否從「授權」標題獲取信息? – Veera

回答

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

這將解決您的問題。