how to use Digest authentication in android? how to use Digest authentication in android? android android

how to use Digest authentication in android?


You need to create a HttpHost and HttpContext object with required credentials and give it to execute method.

This is a sample code where your authentication is independent of backend auth. http client of android will take care of converting it to appropriate format. Check this sample code, this is only for your reference and not to be used directly in your code. :)

This code is in your activity:

@Overridepublic void onResume(){    super.onResume();    AsyncTask<String, Void, Void> httpTask = new TestHttpThread();    httpTask.execute("test_url","test_user","test_password");}

Sample AsyncActivity:

private class TestHttpThread extends AsyncTask<String, Void, Void>{    @Override    protected Void doInBackground(String... params) {       if(params.length > 0){            String url = params[0];            String username = params[1];            String password = params[2];            try {                AndroidHttpClient httpClient = AndroidHttpClient.newInstance("test user agent");                URL urlObj = new URL(url);                HttpHost host = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol());                AuthScope scope = new AuthScope(urlObj.getHost(), urlObj.getPort());                UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);                CredentialsProvider cp = new BasicCredentialsProvider();                cp.setCredentials(scope, creds);                HttpContext credContext = new BasicHttpContext();                credContext.setAttribute(ClientContext.CREDS_PROVIDER, cp);                HttpGet job = new HttpGet(url);                HttpResponse response = httpClient.execute(host,job,credContext);                StatusLine status = response.getStatusLine();                Log.d(TestActivity.TEST_TAG, status.toString());                httpClient.close();            }            catch(Exception e){                e.printStackTrace();            }        }        return null;    }}