当我尝试连接到交换服务器时,出现了401错误:
microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. The request failed. The remote server returned an error: (401)Unauthorized
我使用 https://ewseditor.codeplex.com/ 进行了测试(使用相同的 userId、userPw、domain 和 url),测试结果非常完美。
以下是我的实际代码:
public static void main( String[] args )
{
try {
CustomExchangeService service = new CustomExchangeService(version);
ExchangeCredentials credentials = new WebCredentials(userId, userPw,domain);
service.setUrl(new URI(url));
service.setCredentials(credentials);
service.setTraceEnabled(true);
Folder.bind( getInstance().service, WellKnownFolderName.Inbox, PropertySet.IdOnly );
} catch (Exception e) {
e.printStackTrace();
}
}
这个方法所在的类是CustomExchangeService:
protected Registry<ConnectionSocketFactory> createConnectionSocketFactoryRegistry() {
try {
return RegistryBuilder.<ConnectionSocketFactory>create()
.register(EWSConstants.HTTP_SCHEME, new PlainConnectionSocketFactory())
.register(EWSConstants.HTTPS_SCHEME, EwsSSLProtocolSocketFactory.build(
null, NoopHostnameVerifier.INSTANCE
))
.build();
} catch (GeneralSecurityException e) {
throw new RuntimeException(
"Could not initialize ConnectionSocketFactory instances for HttpClientConnectionManager", e
);
}
}
为什么会出现这个错误?可能是因为 SSL 的原因吗?
谢谢你的帮助。