2013-05-10 59 views
5

我在Android中有一個應用程序,其中我正在從遠程服務器讀取文件,下面給出了讀取文件的代碼;Android中的Microsoft ISA Server身份驗證

  URI uri = null; 
      try { 
       uri = new URI("http://192.168.1.116/Server1/Users.xml"); 
      } catch (URISyntaxException e) { 
       e.printStackTrace(); 
      } 
      HttpGet httpget = new HttpGet(uri); 
      httpget.setHeader("Content-type", "application/json; charset=utf-8"); 
      httpget.setHeader("host", "192.168.1.116"); 

      HttpResponse response = null; 
      try { 
       response = httpClient.execute(httpget); 

      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      HttpEntity responseEntity = response.getEntity(); 
      String result = null; 
      try { 
       result = EntityUtils.toString(responseEntity); 
       Log.d("SERVER1", result); 
      } catch (ParseException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

現在所有遠程文件都在代理(Microsoft ISA Server)後面,它需要身份驗證來訪問文件。請指導我如何從android傳遞身份驗證參數以訪問文件。

我曾嘗試以下代碼,但沒用,

  URL uri = null; 
      try { 
       uri = new URI("http:// 192.168.1.116/CookieAuth.dll?Logon"); 
      } catch (URISyntaxException e) { 
       e.printStackTrace(); 
      } 

      DefaultHttpClient httpclient = new DefaultHttpClient(); 
      httpclient.getCredentialsProvider() 
        .setCredentials(
          AuthScope.ANY, 
          new NTCredentials(username, password, deviceIP, 
            domainName)); 

      HttpPost httppost = new HttpPost(uri); 
      httppost.setHeader("Content-type", 
        "application/x-www-form-urlencoded"); 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("curl", "Z2FServer1/users.xmlZ2F")); 
      params.add(new BasicNameValuePair("formdir", "3")); 
      params.add(new BasicNameValuePair("username", "test")); 
      params.add(new BasicNameValuePair("password", "test")); 

      UrlEncodedFormEntity ent = null; 
      try { 
       ent = new UrlEncodedFormEntity(params, HTTP.UTF_8); 
      } catch (UnsupportedEncodingException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      httppost.setEntity(ent); 
      try { 
       response = httpclient.execute(httppost); 

       Log.i("Test", response.getStatusLine().toString()); 

       HttpEntity entity = response.getEntity(); 

       if (entity != null) { 

        InputStream instream = entity.getContent(); 
        result = convertStreamToString(instream); 
        Log.d("ISA Server", result); 

        instream.close(); 
       } 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

但它給HTTP1.1 500內部服務器錯誤。我也曾嘗試下面的鏈接,但同樣的錯誤 https://stackoverflow.com/a/10937857/67381

回答

1

在我來說,我使用

public static DefaultHttpClient getClient(String username, String password, 
     Integer timeOut) { 
    HttpParams httpParams = new BasicHttpParams(); 
    HttpConnectionParams.setConnectionTimeout(httpParams, timeOut); 
    HttpConnectionParams.setSoTimeout(httpParams, timeOut); 
    DefaultHttpClient retHttpClient = new DefaultHttpClient(httpParams); 
    if (username != null) { 
     retHttpClient.getCredentialsProvider().setCredentials(
       new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), 
       new UsernamePasswordCredentials(username, password)); 
    } 

    return retHttpClient; 
} 

嘗試,可能是作品。

P.S.

它不是爲ISA,用於MS C#WebService的應用程序,但也許......

+0

我收到HTTP/1.1 500(未指定的錯誤)。 – Siddiqui 2013-06-06 11:26:08

+0

服務器日誌?可能會在這裏例外嗎? – 2013-06-06 12:36:11