在一个月的大部分时间里,我一直在尝试弄清楚如何通过 Android 登录我所在大学的 CAS 系统。我对 HTTP 和身份验证一窍不通,我花了很多时间在谷歌上搜索术语,但现在我已经完全掌握了它们。
无论如何,我正在尝试登录 CAS 系统,并且我能够找到我在下面发布的系统工作原理图。
到目前为止,我有以下代码:
DefaultHttpClient httpClient = new DefaultHttpClient();
try {
HttpGet httpGet = new HttpGet(LOGIN);
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
Log.i(TAG, "Login form get: " + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}
Log.i(TAG, "Initial set of cookies: ");
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
Log.i(TAG, "THere are no cookies");
}
else {
for (int i = 0; i < cookies.size(); i++) {
Log.i(TAG, " - " + cookies.get(i).toString());
}
}
HttpPost httpPost = new HttpPost(LOGIN);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username", "MyUserName"));
nvps.add(new BasicNameValuePair("password", "MyPassword"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
Log.i(TAG, "The last header's value is.... " + response.getAllHeaders().length);
response = httpClient.execute(httpPost);
entity = response.getEntity();
Log.i(TAG, "Login form get: " + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}
Log.i(TAG, "Post login cookies: ");
cookies = httpClient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
Log.i(TAG, "No Cookies");
}
else {
for (int i = 0; i < cookies.size(); i++) {
Log.i(TAG, " - " + cookies.get(i).toString());
}
}
httpClient.getConnectionManager().shutdown();
此代码不会返回任何错误,但它只会抓取一个 cookie,当我认为我还需要一个时,CASTGC cookie......
我希望比我更有经验的人能够理解上面的图表,并至少告诉我我是否在正确的轨道上。
感谢阅读。
最佳答案
如果您对 CAS RESTful API 感兴趣: https://wiki.jasig.org/display/CASUM/RESTful+API
RESTful API 遵循与原始 CAS2 协议(protocol)相同的基本协议(protocol),并增加了一些额外的明确定义的资源 URL。这对于需要以编程方式访问 CAS 的应用程序特别有用。
希望这对您有所帮助。
关于android - 如何通过Android登录本CAS系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8793255/