Search...

Sunday, November 13, 2011

How to make HttpDigest client authentication call in android


public void testDigest(){
         String url = "http://1.1.1.1:8080/mydata?client=0";
          String username = "test";
          String password = "test";
           
           
         try
       {
           AndroidHttpClient httpClient = AndroidHttpClient.newInstance("test digest");
         
           URL urlObj = new URL(url);
               HttpGet request = new HttpGet(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);
                      
                       HttpResponse response = httpClient.execute(host,request,credContext);
                      
                       ByteArrayOutputStream v2 = new ByteArrayOutputStream();
                              response.getEntity().writeTo(v2);
                              System.out.println("----------------------------------------");
                              System.out.println(v2.toString());
                              System.out.println("----------------------------------------");
                              System.out.println(response.getStatusLine().getReasonPhrase());
                              System.out.println(response.getStatusLine().getStatusCode());
                       
                       
                  //}
          
         

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

No comments:

Post a Comment