使用Java中的自动发现连接到Exchange Server

时间:2017-01-26 02:23:25

标签: java exchange-server

我们正在尝试使用自动发现网址连接到我们的Exchange服务器。使用REST客户端运行时,url返回wsdl文件:

System.setProperty("javax.net.ssl.trustStore", "truststore.jks");
System.setProperty("javax.net.ssl.trustStoreType", "JKS");

KeyStore trustStore = KeyStore.getInstance("JKS");
trustStore.load(new FileInputStream("truststore.jks"), null);
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);

SSLContext ctx = SSLContext.getInstance("SSL");
ctx.init(null, trustManagerFactory.getTrustManagers(), null);

HostnameVerifier hostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
client = Client.create(config);
config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(hostnameVerifier, ctx));
webResource = client.resource("https://autodiscover.company.com/xxxx/autodiscover.svc");
client.addFilter(new HTTPBasicAuthFilter(username, password));

但是在使用交换服务运行时它返回401未经授权的错误:

System.setProperty("javax.net.ssl.trustStore", "truststore.jks");
System.setProperty("javax.net.ssl.trustStoreType", "JKS");
KeyStore trustStore = KeyStore.getInstance("JKS");
trustStore.load(new FileInputStream("truststore.jks"),null);
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);

SSLContext ctx = SSLContext.getInstance("SSL");
ctx.init(null, trustManagerFactory.getTrustManagers(), null);

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
service.setTraceEnabled(true);
service.setPreAuthenticate(true);
ExchangeCredentials credentials = new WebCredentials(username,password,domain);
service.setCredentials(credentials);
service.setUserAgent(user);
service.setUrl(new java.net.URI("https://autodiscover.company.com/xxxx/autodiscover.svc"));

以下是堆栈跟踪:

microsoft.exchange.webservices.data.HttpErrorException: The remote server returned an error: (401)Unauthorize
at microsoft.exchange.webservices.data.ServiceRequestBase.validateAndEmitRequest(ServiceRequestBase.java:729)
    at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:36)
    at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:140)
    at microsoft.exchange.webservices.data.ExchangeService.findItems(ExchangeService.java:945)
    at microsoft.exchange.webservices.data.ExchangeService.findItems(ExchangeService.java:998)
    at microsoft.exchange.webservices.data.ExchangeService.findItems(ExchangeService.java:1070)

请建议。

1 个答案:

答案 0 :(得分:0)

将ews-java-api jar版本更改为2.0并完成了诀窍: - )