gpt4 book ai didi

java - SOAP 授权

转载 作者:行者123 更新时间:2023-11-29 03:31:40 26 4
gpt4 key购买 nike

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

最佳答案

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

   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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com