gpt4 book ai didi

Android - session Cookie

转载 作者:行者123 更新时间:2023-11-29 22:11:57 24 4
gpt4 key购买 nike

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

我正在从表单上的 EditText 中收集用户名和密码,并将其传递给身份验证 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);

}

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

public static 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;
}

在 Manifest 文件中我设置了权限。

没有错误,但最后返回 NULL。我已经检查(在调试中)函数中传递的用户名和密码是否正确。

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

谢谢。

最佳答案

查看此链接:How do I make an http request using cookies on Android?

我认为这是设置和获取 cookie 的首选方式,而不是像您那样循环遍历 header (检查函数的末尾):

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

/**
* A example that demonstrates how HttpClient APIs can be used to perform
* form-based logon.
*/
public class ClientFormLogin {

public static void main(String[] args) throws Exception {

DefaultHttpClient httpclient = new DefaultHttpClient();

HttpGet httpget = new HttpGet("https://portal.sun.com/portal/dt");

HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();

System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}
System.out.println("Initial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}

HttpPost httpost = new HttpPost("https://portal.sun.com/amserver/UI/Login?" +
"org=self_registered_users&" +
"goto=/portal/dt&" +
"gotoOnFail=/portal/dt?error=true");

List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("IDToken1", "username"));
nvps.add(new BasicNameValuePair("IDToken2", "password"));

httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

response = httpclient.execute(httpost);
entity = response.getEntity();

System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}

System.out.println("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}

// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}

关于Android - session Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9416712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com