android - 如何通过Android登录本CAS系统

标签 android http authentication cookies cas

在一个月的大部分时间里,我一直在尝试弄清楚如何通过 Android 登录我所在大学的 CAS 系统。我对 HTTP 和身份验证一窍不通,我花了很多时间在谷歌上搜索术语,但现在我已经完全掌握了它们。

无论如何,我正在尝试登录 CAS 系统,并且我能够找到我在下面发布的系统工作原理图。

http://www.middleware.vt.edu/lib/exe/detail.php?id=middleware%3Acas%3Aprotocol&media=middleware:cas:cas_v1_authentication.png

到目前为止,我有以下代码:

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/

相关文章:

javascript - 使用 Node 下载 .ttf 文件

带有自签名无效证书的 http2

java - 可以在 HttpServletResponse.sendRedirect() 中发送隐藏参数吗?

android - APNS 是否理解 UTF-8 编码的有效负载

android - “getActionId overrides nothing” 升级到导航版本 2.4.0-alpha02 后

android - 当我使其中一个 View 无效时,为什么我的布局会完全重绘?

php - 为什么在 laravel 中按下浏览器的后退按钮时过滤器不起作用?

java - SOAP 客户端基本身份验证 : HTTP response '401: Unauthorized'

php - 在 PHP 中创建登录系统

java - 如何从 json 字符串中获取值