使用凭据通过https从Android调用WCF

时间:2012-04-16 15:54:19

标签: android wcf https

我试图通过Android上的https调用我的RESTful WCF服务。每当我拨打电话时,我都会收到401:未经授权的错误。我的代码的其他部分工作,我在本地测试它。

以下是相关代码:

// http scheme
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
// https scheme
schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials("user", "pass"));
clientConnectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);

context = new BasicHttpContext();
context.setAttribute("http.auth.credentials-provider", credentialsProvider);

HttpGet request = new HttpGet(SERVICE_URI + URL1 + EID);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");

DefaultHttpClient client = new DefaultHttpClient(clientConnectionManager, params);
client.setCredentialsProvider(credentialsProvider);
HttpResponse response = client.execute(request, context);

HttpEntity responseEntity = response.getEntity();

EasySSLSocketFactory使用X509TrustManager的实现。我知道代码也可以。有人可以提供一些建议。我找不到任何可行的工作。

1 个答案:

答案 0 :(得分:0)

事实证明,HttpClient不支持开箱即用的NTLM身份验证(我使用带有Windows身份验证的IIS)。这个链接给了我所需要的一切。只需复制并粘贴即可忘记它。

http://hc.apache.org/httpcomponents-client-ga/ntlm.html