0

I am trying to call my RESTful WCF service over https on Android. I keep getting a 401: unauthorized error whenever I make the call. The other parts of my code work, ive tested it locally.

Here is the pertinent code:

// 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 uses an implementation of X509TrustManager. I know that code works as well. Can someone please provide some advice. Nothing I have found works correctly.

4

1 回答 1

0

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

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

于 2012-04-18T18:03:34.757 回答