客户端应用程序中的身份验证(HttpUrlConnection和基本身份验证)

时间:2013-04-03 11:28:24

标签: java android authentication httpurlconnection basic-authentication

我正在开发REST服务,现在我正在做一个客户端应用程序。我是用Java做的,我正在测试它,然后我想把它复制到Android应用程序。

我的疑问是在身份验证中。我正在使用HttpUrlConnection,我这样做:

 final String name = "name";
 final String password = "pass";

 Authenticator.setDefault(new Authenticator()
 {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() { 
          return new PasswordAuthentication(name,password.toCharArray()); }
 });

  URL urlToRequest = new URL(webPage);
  String basicAuth = "Basic " + new String(Base64.encodeBase64((name + ":" + password).getBytes()));
  urlConnection = (HttpURLConnection) urlToRequest.openConnection();
  urlConnection.setRequestProperty ("Authorization", basicAuth);
  System.out.println("Authorization : " + basicAuth);

我想知道我是否正在通过正确的顺序执行所有步骤,以及当我将此代码放入Android应用程序时它是否可行。我读过有关使用Base64.NO_WRAP的内容,但我无法使用它。它告诉我“找不到符号”。怎么用?

由于

0 个答案:

没有答案