2012-05-25 49 views
0

我正在開發一個Android應用程序,它與我的服務器中的RESTful WCF Web服務進行通信。
通過使用HttpClient,應用程序可以從url鏈接讀取json代碼。
例如:對ISA Server的身份驗證,使用HttpClient(Android)

http://www.example.com/WebService/Service.svc/subscriptions/tiganeus
返回{ 「JSONUserDataResult」: 「應用程序A」, 「應用程序B」]}

然而,這個Web服務本身是匿名的訪問,而是通過ISA服務器的保護。
當外部訪問此鏈接時,瀏覽器會自動顯示「Authentication Required」對話框。只需填寫用戶名和密碼即可。

我想出瞭如何在web視圖中進行身份驗證。下面的代碼工程

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"); 
    } 
} 

但我真正需要的是從url讀取JSON代碼。 我選擇了使用HttpClient來完成這項工作,但我無法弄清楚如何在HttpClient中進行身份驗證。這聽起來很簡單,因爲任何瀏覽器都可以。

任何幫助將不勝感激。

回答

0

顯然它比我想象的要簡單得多。

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方法,至少在我的ISA服務器的配置方式工作正常,通過ISA Server (鑑別。

它處理基本身份驗證爲好。