如何在android中进行http认证

时间:2011-09-22 12:55:30

标签: android http-authentication

我正在使用以下代码

 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();


    }

现在我想对此进行http认证,请帮帮忙?

2 个答案:

答案 0 :(得分:2)

它应该是这样的:

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

答案 1 :(得分:0)

对于http身份验证,Authenticator类是您的朋友。