首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过客户端存根访问WSDL时的InaccessibleWSDLException

通过客户端存根访问WSDL时的InaccessibleWSDLException
EN

Stack Overflow用户
提问于 2013-10-08 11:32:43
回答 2查看 38.1K关注 0票数 5

我正在尝试为Exchange服务编写自定义Java客户端。我使用wsimport工具从EWS的Services.wsdl文件中生成了解释这里的客户端存根。现在我已经编写了使用这些存根的代码。我得到以下例外:

代码语言:javascript
复制
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。但是在堆栈跟踪中,我从未知来源(大概是JDKs的JAR)获得了错误。

IOException说它得到了HTTP response code: 401,这是用于未经授权的访问。但是,我正确地指定了用户名和密码,并在密钥库中包含了所需的证书。我完全没有方向这个例外是从哪里来的。

我编写的类代码:

ExchangeAuthenticator

代码语言:javascript
复制
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

代码语言:javascript
复制
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

使用wsimport工具生成JAX,删除其他构造函数和方法。只有第58行中调用super (此处为Service类)构造函数的构造器才被保留。

代码语言:javascript
复制
@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);
    }    
}
EN

回答 2

Stack Overflow用户

发布于 2013-10-08 14:30:12

当您可以拥有一个本地副本时,为什么要访问远程WSDL文档文件(和模式文件)?当然,访问端点仍然需要安全性。

首先,根据环境需要类加载器。

代码语言:javascript
复制
// Java EE Enviroment
ClassLoader cl = Thread.currentThread().getContextClassLoader();

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

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

代码语言:javascript
复制
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();

如果访问its服务端点需要身份验证,则其最基本的形式如下:

代码语言:javascript
复制
BindingProvider provider = (BindingProvider) port;
Map<String, Object> context = provider.getRequestContext();
context.put(BindingProvider.USERNAME_PROPERTY, username);
context.put(BindingProvider.PASSWORD_PROPERTY, password);

如果您需要处理证书之类的问题,最好看看http://docs.oracle.com/cd/E12839_01/web.1111/e13713/toc.htm

票数 6
EN

Stack Overflow用户

发布于 2013-10-08 11:49:12

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

代码语言:javascript
复制
    ShopingCart sc = scs.getShopingCartPort();
    Map requestContext = ((BindingProvider)sc).getRequestContext();
    requestContext.put(BindingProvider.USERNAME_PROPERTY, userName);
    requestContext.put(BindingProvider.PASSWORD_PROPERTY, password);

您没有给出您的客户端代码。你在使用代理吗?然后,您必须在上面给出代理用户名和密码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19246608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档