Java通过客户端存根访问WSDL时出现InaccessibleWSDLException异常

5

我正在尝试编写Exchange Web Services的自定义Java客户端。我已经使用 wsimport 工具根据EWS的Services.wsdl文件生成了客户端存根,如此处所述。现在,我编写了使用这些存根的代码。我遇到了以下异常:

Exception in thread "main" com.sun.xml.internal.ws.wsdl.parser.InaccessibleWSDLException: 2   counts of InaccessibleWSDLException.

java.io.IOException: Got Server returned HTTP response code: 401 for URL: https://host.domain.com/ews/Services.wsdl while opening stream from https://host.domain.com/ews/Services.wsdl
java.io.IOException: Got Server returned HTTP response code: 401 for URL: https://host.domain.com/ews/Services.wsdl?wsdl while opening stream from https://host.domain.com/ews/Services.wsdl?wsdl

at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(Unknown Source)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(Unknown Source)
at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(Unknown Source)
at javax.xml.ws.Service.<init>(Unknown Source)
at com.microsoft.schemas.exchange.services._2006.messages.ExchangeWebService.<init>(ExchangeWebService.java:58)
at com.xyz.abc.EWSJavaAPI.ExchangeAuthenticator.getExchangeServicePort(ExchangeAuthenticator.java:33)
at com.xyz.abc.test.ExchangeDevelopmentTest.main(ExchangeDevelopmentTest.java:35)

正如我们在上面看到的,ExchangeDevelopmentTest 是一个客户端,它使用另一个类 ExchangeAuthenticator,而这个类又使用了生成的客户端存根 ExchangeWebService。但是在堆栈跟踪中,我收到了来自未知来源的错误,可能是JDK的JAR文件产生的。

IOException 表示它得到了 HTTP response code: 401,这是由于未经授权的访问。但是我已经正确地指定了用户名和密码,并且在密钥库中包含了所需的证书。我完全不知道这个异常是从哪里来的。

我编写的类的代码:

ExchangeAuthenticator

public class ExchangeAuthenticator {    
/**
 * Obtains an authenticated ExchangeServicePortType with given credentials.
 *     
 */
    public ExchangeServicePortType getExchangeServicePort(String username, String password, String domain, URL wsdlURL) throws MalformedURLException {
        // Concatinate our domain and username for the UID needed in authentication.
        String uid = "domain" + "\\" + "uname";

        // Create an ExchangeWebService object that uses the supplied WSDL file, wsdlURL.
        ExchangeWebService exchangeWebService = new ExchangeWebService(wsdlURL, new QName("<a href=\"http://schemas.microsoft.com/exchange/services/2006/messages\">http://schemas.microsoft.com/exchange/services/2006/messages</a>", "ExchangeWebService"));
        ExchangeServicePortType port = exchangeWebService.getExchangeWebPort();
        // Supply your username and password when the ExchangeServicePortType is used for binding in the SOAP request.
        ((BindingProvider)port).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, uid);
        ((BindingProvider)port).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);

        return port;
    }
}

ExchangeDevelopmentTest

public class ExchangeDevelopmentTest {    
    public static void main (String[] args) {
        ExchangeAuthenticator exchangeAuthenticator = new ExchangeAuthenticator();

        // Print statement so we can easily see where our statements start in the Java console.
        System.out.println("Let's get started!");

        try {
            // Create a URL object which points at the .wsdl we deployed in the previous step.
            URL wsdlURL = new URL("https://172.17.245.196/ews/Services.wsdl");
            //URL wsdlURL = new URL("<a href=\"https://172.17.245.196/ews/Services.wsdl\">https://172.17.245.196/ews/Services.wsdl</a>");
            // Call to the class we just created to return an ExchangeServicePortType with authentication credentials.
            ExchangeServicePortType port = exchangeAuthenticator.getExchangeServicePort("uname", "password@123", "domain", wsdlURL);

            // Prints out the default toString() for the ExchangeServicePortType.
            System.out.println(port.toString());
        } catch (MalformedURLException ex) {
            // Catch any errors that may occur.
            Logger.getLogger(ExchangeDevelopmentTest.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(ex.getMessage()+"\n"+ex.getStackTrace());
        }
    }
}

ExchangeWebService

这是一个由JAX-WS通过wsimport工具生成的类。为了简化内容,我们删除了其他构造函数和方法,只保留了第58行调用父类(这里是Service类)构造函数的构造函数。

@WebServiceClient(name = "ExchangeWebService", targetNamespace =     "http://schemas.microsoft.com/exchange/services/2006/messages", wsdlLocation = "file:/C:/Services.wsdl")
public class ExchangeWebService extends Service
{ 
    private final static URL EXCHANGEWEBSERVICE_WSDL_LOCATION;
    private final static WebServiceException EXCHANGEWEBSERVICE_EXCEPTION;
    private final static QName EXCHANGEWEBSERVICE_QNAME = new QName("http://schemas.microsoft.com/exchange/services/2006/messages", "ExchangeWebService");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = new         URL("file:/C:/workspace/Server%20files/Client%20files/Services.wsdl");
        } catch (MalformedURLException ex) {
            e = new WebServiceException(ex);
        }
        EXCHANGEWEBSERVICE_WSDL_LOCATION = url;
        EXCHANGEWEBSERVICE_EXCEPTION = e;
    }

    //other constructos & methods removed
    //line 58
    public ExchangeWebService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }    
}
2个回答

6
为什么要访问远程WSDL文档文件(以及模式文件),当你可以拥有一个本地副本呢?当然,访问端点仍然需要安全性保障。
首先,你需要根据环境获取类加载器。
// Java EE Enviroment
ClassLoader cl = Thread.currentThread().getContextClassLoader();

// Java Standalone Enviroment
ClassLoader cl = ClassLoader.getSystemClassLoader();

接下来,在您的项目中本地存储WSDL文档文件的副本(如果需要,还要存储方案文件)。

URL wsdlLocation = cl.getResource("com/mahesha999/ExchangeWebService.wsdl");
QName qName = new QName(
    "http://schemas.microsoft.com/exchange/services/2006/messages", 
    "ExchangeWebService"
);

ExchangeWebService exchangeWebService = new ExchangeWebService(wsdlLocation, 
        qName);
ExchangeServicePortType port = exchangeWebService.getExchangeWebPort();

如果需要身份验证才能访问我们bservice端点,其最基本的形式如下:

BindingProvider provider = (BindingProvider) port;
Map<String, Object> context = provider.getRequestContext();
context.put(BindingProvider.USERNAME_PROPERTY, username);
context.put(BindingProvider.PASSWORD_PROPERTY, password);

如果您需要处理证书等类似问题,请查看保护WebLogic Web服务


1

你是这样添加用户名和密码的吗?

    ShopingCart sc = scs.getShopingCartPort();
    Map requestContext = ((BindingProvider)sc).getRequestContext();
    requestContext.put(BindingProvider.USERNAME_PROPERTY, userName);
    requestContext.put(BindingProvider.PASSWORD_PROPERTY, password);

您在问题中没有提供客户端代码。您是否使用代理?如果是,则需要在上面提供您的代理用户名和密码。

我已经添加了代码。正如您所看到的,ExchangeDevelopmentTest 包含 main()。在 ExchangeAuthenticator 的第 ExchangeWebService exchangeWebService... 行中,我遇到了异常。这一行只是调用了 ExchangeWebService 类(由 wsimport 工具生成)的一个构造函数,该构造函数又调用了 super(这里是 Service 类)构造函数。在调试时,我无法进入此类的源代码(显然)。 - Mahesha999
在第二个参数的构造函数中使用QName,就像自动生成的代码提供的那样。new QName("http://schemas.microsoft.com/exchange/services/2006/messages", "ExchangeWebService") - Satish

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接