【发布时间】:2012-03-14 01:47:19
【问题描述】:
我需要通过提供会话 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。
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;
}
在 Manifest 文件中我设置了权限。
没有错误,但最后会重新调整 NULL。我已经检查(在调试中)在函数中传递正确的用户名和密码。
我希望有人可以在这里提供帮助。
谢谢。
【问题讨论】:
标签: android login http-post session-cookies