0

我正在尝试使用 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>

我猜我已经对自己进行了身份验证,但不是我的网络服务有问题。但是,我并不完全确定。此错误消息似乎并不常见。

这是否意味着我提供的身份验证不正确或不充分?还是我必须提供 SSL 证书,因为 Web 服务使用 SSL?如果是,是否有关于如何在 SAAJ 中使用 SSL 证书的教程?

4

1 回答 1

0

问题是我使用“ https://example.com/xxx.php ”作为端点而不是“ https://example.com/xxx.php?wsdl ”。这就是它无法加载任何 wsdl 文件的原因。

于 2016-02-11T16:37:36.310 回答