我正在开发一个能够从 Lotus Domino 数据库读取数据的 android 应用程序。 我开始创建一个页面来测试 HTTP 身份验证,但遇到了很多困难。这是我的代码 fragment :
public void GoAuth(View v){
final String httpsURL = "http://xxx.xxx.xxx.xxx/names.nsf/mypage?openpage";
final DefaultHttpClient client = new DefaultHttpClient();
final HttpPost httppost = new HttpPost(httpsURL);
String userName = "demo";
String password = "demo";
try {
//authentication block:
final List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("Username", userName));
nvps.add(new BasicNameValuePair("Password", password));
final UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
httppost.setEntity(p_entity);
//sending the request and retrieving the response:
HttpResponse response = client.execute(httppost);
HttpEntity responseEntity = response.getEntity();
if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK){
//handling the response
final InputSource inputSource = new InputSource(responseEntity.getContent());
TextView res=(TextView)findViewById(R.id.result);
res.setText("Server response: "+inputSource.toString());
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
服务器响应是: org.xml.sax文件 InputSource@40575700
在浏览器中尝试同样的操作,我看到了登录页面,然后是“mypage”的内容。 我对在 android 上必须遵循的正确方法和机制感到有点困惑。 任何帮助将不胜感激!
最佳答案
正如 Richard 在评论中提到的,您可能会看到“session based authentication 表单”,这对于处理任何类型的代码来说都相当麻烦。
为了获得可能任何语言都可以轻松处理的“HTTP 基本身份验证”(基于浏览器的用户名/密码提示),您可以/应该实现 Override Session Authentication Rule在服务器端。
另见 Domino 7.0.2 allows for overriding of session-based authentication
关于Lotus Domino 服务器上的 Android HTTP 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10139345/