Android - 会话 Cookie
我需要使用提供会话 Cookie 的网站来验证用户名和密码。
我从表单上的 EditText 收集用户名和密码,并将其传递到身份验证会话。
@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。
公共静态 String getSessionCookie(字符串用户名,字符串密码) {
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;
}
在清单文件中我设置了权限。
没有错误,但最后返回 NULL。我已经检查(在调试中)在函数中传递的用户名和密码是否正确。
我希望有人可以在这里提供帮助。
谢谢。
I need to authenticate username and password with my website which provides Session Cookies.
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);
}
The username and password are then checked if they are correct to return the session cookie.
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.
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.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看此链接:如何制作在Android上使用cookie的http请求?
我认为这是设置和获取cookie的首选方式,而不是像你那样循环遍历标头(检查函数的末尾):
Check this link out: How do I make an http request using cookies on Android?
I think that is the preferred way of setting and getting cookies, not looping through the headers like you do (check the end of the function):
对于任何有兴趣的人;这是工作代码。它展示了它的方法。
For anyone interested; here is the working code. Its an exert of it showing the method.