Authentication against ISA Server, Using HttpClient (Android)

不打扰是莪最后的温柔 提交于 2019-12-08 11:54:17

问题


I am developing a Android app, which communicates with a RESTful WCF Web Service in my server.
By using HttpClient, the application can read the json code from a url link.
For Example:

http://www.example.com/WebService/Service.svc/subscriptions/tiganeus
returns {"JSONUserDataResult":["Application A","Application B"]}

However, this web service itself is anonymous accessible, but protected by ISA Server.
Browser automatically shows "Authentication Required" dialog when this link is accessed externally. Simply fill in the username and password is OK.

I figured out how to do authentication in a webview. The following code works

private class MyWebViewClient extends WebViewClient {
    @Override
    public void onReceivedHttpAuthRequest(WebView view,
            HttpAuthHandler handler, String host, String realm) {
        handler.proceed("username", "password");
        System.out.println("httpa");
    }
}

But what I really need is to read the JSON code from the url. I have chosen to use HttpClient to do the job, but I can't figure out how to authenicate within the HttpClient. It sounds simple as any browser can do it.

Any help will be appreciated.


回答1:


Apparently it is much simpler than I thought.

DefaultHttpClient() httpClient = new DefaultHttpClient();       

httpClient.getCredentialsProvider().setCredentials(
    AuthScope.ANY,
    new NTCredentials(user, pass, "tamtam", "tamtam"));         

URI uri = new URI("http://www.example.com/WebService/Service.svc/subscriptions/tiganeus");
HttpGet httpget = new HttpGet(uri);
httpget.setHeader("Content-type", "application/json; charset=utf-8");*/

HttpResponse response = httpClient.execute(httpget);

HttpEntity responseEntity = response.getEntity();
String result = EntityUtils.toString(responseEntity);

httpClient.getCredentialsProvider().setCredentials works fine for the authentification through isa server (at least for in the way my isa-server is configured.

It handles basic authentication as well.



来源:https://stackoverflow.com/questions/10761911/authentication-against-isa-server-using-httpclient-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!