![](/img/trans.png)
[英]Login implementation for web application consuming authenticated REST service
[英]Consuming an authenticated web service using SAAJ?
我正在嘗試使用SAAJ使用經過身份驗證的Web服務。 這是我到目前為止的代碼:
import java.io.ByteArrayOutputStream;
import javax.xml.soap.*;
import biz.source_code.base64Coder.*;
public class Client {
private static String endpoint = "https://example.com/xxx.php",
username = "xxx", password = "xxx";
private static SOAPMessage getRequest() throws Exception{
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
//set authorization as a HTTP header
String authorization = Base64Coder.encodeString(username + ":" + password);
MimeHeaders hd = message.getMimeHeaders();
hd.addHeader("Authorization", "Basic " + authorization);
//Call getReportList operation
return message;
}
public static void main(String[] args) throws Exception {
SOAPConnectionFactory connFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = connFactory.createConnection();
// create request message and give it content
SOAPMessage request = Client.getRequest();
// call the API endpoint with the request
SOAPMessage response = connection.call(request, endpoint);
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.writeTo(out);
String strMsg = new String(out.toByteArray());
System.out.println(strMsg);
}
}
當我運行它時,它按如下方式打印strMsg(來自Web服務的響應):
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>WSDL</faultcode><faultstring>SOAP-ERROR: Parsing WSDL: Couldn't load from '/www/example.wsdl' : failed to load external entity "/www/example.wsdl"
</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>
我猜我已經對自己進行了身份驗證,但是Web服務存在問題,不是我的。 但是,我不確定。 此錯誤消息似乎並不常見。
這是否表示我提供的身份驗證不正確或不足? 還是因為Web服務使用SSL而必須提供SSL證書? 如果是,是否有關於如何在SAAJ中使用SSL證書的教程?
問題是我使用“ https://example.com/xxx.php ”作為端點,而不是“ https://example.com/xxx.php?wsdl ”。 這就是為什么它無法加載任何wsdl文件的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.