Search code examples
androidhttp-authentication

How to do http authentication in android


I am using following code

 public void executeHttpGet() throws Exception {
    dialog = new ProgressDialog(this);
    dialog.setCancelable(true);

    // set a message text
    dialog.setMessage("Loading...");

    // show it
    dialog.show();
     BufferedReader in = null;

            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
         String url=   "http://newdev.objectified.com/morris/interface/mobile.php?method=dealerLogin&username=alixxxxxxxx&password=jamali";


            request.setURI(new URI(url));
            HttpResponse response = client.execute(request);
            in = new BufferedReader
            (new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();
            String page = sb.toString();
            Toast.makeText(this, page, Toast.LENGTH_LONG).show();
         //  dialog.dismiss();


    }

Now I want to do http authentication on this ,please help?


Solution

  • It should look like this:

         HttpClient client = new DefaultHttpClient();
         HttpGet request = new HttpGet();
         String url=   "http://newdev.objectified.com/morris/interface/mobile.php?method=dealerLogin&username=alixxxxxxxx&password=jamali";
    
         String login = "alixxxxxxxx";
         String pass = "jamali";
    
         client.getCredentialsProvider().setCredentials(new AuthScope("newdev.objectified.com", 80), new UsernamePasswordCredentials(login, pass));
    
         request.setURI(new URI(url));
         HttpResponse response = client.execute(request);