【发布时间】:2017-02-07 18:41:17
【问题描述】:
当我尝试连接到 Exchange 服务器时,出现 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、域和 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 的事情?
感谢您的帮助
【问题讨论】:
-
该错误与凭据有关,如果它是 SSL,您将返回一个不同的错误。检查您的凭据格式,您可能只想尝试使用 UPN 作为用户名,然后您只需要 UserName,Password 作为凭据
-
已解决:在服务器上,打开 Internet 信息服务 (IIS) 管理器。在左侧部署以找到 EWS。单击身份验证并使用 401 激活所有行。
标签: java api exchange-server exchangewebservices