我需要使用提供會話Cookie的網站驗證用戶名和密碼。Android - 會話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。
公共靜態字符串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;
}
清單文件我有權限設置。
有沒有錯誤,但空是在年底重新調整。我已經檢查了(在調試中)在函數中正確傳遞的用戶名和密碼。
我希望有人能幫到這裏。
謝謝。
讓我試試這種方法,但它並沒有說什麼關於內容類型! – 2012-02-23 16:42:46
我需要以特定格式將用戶名和密碼作爲XML文檔發送。 String sessionXML =「」+ username +「 」+ password +「 」; –
2012-02-25 23:17:13
我還需要將Content-Type設置爲「application/xml」;我如何使用NameValuePairs來完成這些操作?你能幫我嗎 ? – 2012-02-25 23:18:27