Android - 会话Cookie [英] Android - Session Cookies

查看:106
本文介绍了Android - 会话Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过提供会话Cookie的网站验证用户名和密码。

I need to authenticate username and password with my website which provides Session Cookies.

我从表单上的EditText收集用户名和密码,并将其传递给身份验证会话。

I am collecting username and password from the EditText on the form and passing it onto authenticate session.

    @Override
    public void onClick(View v) 
    {
        String Username =  username.getText().toString();
        String Password = password.getText().toString();
        String value = LoginAuthenticate.getSessionCookie(Username, Password);
        //This is just check what session value is brought back
        username.setText(value);

    }

然后检查用户名和密码是否正确返回会话cookie。

The username and password are then checked if they are correct to return the session cookie.

public static String getSessionCookie(String username,String password)
{

public static String getSessionCookie(String username, String password) {

    String login_url = "http://www.myexperiment.org/session/create";
    URLConnection connection = null;
    String sessionXML = "<session><username>" + username +
            "</username><passsword>" + password +
            "</password></session>";
    String cookieValue = null;
    try 
    {

        URL url = new URL(login_url);
        connection = url.openConnection();

        connection.setRequestProperty("Content-Type", "application/xml");

        connection.setDoOutput(true);

        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write(sessionXML);

        out.close();
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
        return null;
    } 
    String headerName = null;
    for (int i =0; (headerName = connection.getHeaderFieldKey(i)) != null; i++)
    {
        if(headerName.equals("Set-Cookie"))
        {
            cookieValue = connection.getHeaderField(i);
        }
    }

    //return connection.getHeaderField("Set-Cookie:");
    return cookieValue;
}

在清单文件中,我有权限设置。

In the Manifest file I have permission set.


没有错误,但最后会重新调整NULL。我已经检查了(在调试中)在函数中传递正确的用户名和密码。

There are no errors but NULL is retuned at the end. I have checked (in debug) the username and password which are correct being passed in the function.

我希望有人可以在这里提供帮助。

I hope someone can help here.

谢谢。

推荐答案

对于任何有兴趣的人;这是工作代码。它显示了该方法。

For anyone interested; here is the working code. Its an exert of it showing the method.

try
{
    URL url = new URL(login_url);
    connection = (HttpURLConnection) url.openConnection();  

    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/xml");

    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
    out.write(sessionXML);
    out.flush();
    out.close();

    String headerName = "";

    for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null; i++)
    {       
        if(headerName.equals("Set-Cookie"))
        {
            cookieValue = connection.getHeaderField(i);         
        }
    }
} 
catch (Exception e)
{
    e.printStackTrace();
}
finally
{
    if(connection != null)
        connection.disconnect();
}

这篇关于Android - 会话Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆