gpt4 book ai didi

android - 通过 httpget 发送 cookie 值但没有得到所需的响应

转载 作者:可可西里 更新时间:2023-11-01 16:38:23 26 4
gpt4 key购买 nike

我按如下方式调用我的登录网络服务

String url = "http://mydomaim.com/login.php";

UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser(userEmail, password, url);

它工作正常并向我发送如下所示的响应

{
"userName":"a",
"login_success":1,
"user_id":"3",
"session_id":"1067749aae85b0e6c5c5e697b61cd89d",
"email":"a"
}

我解析了这个响应,并成功地获得了 session ID,并将它存储在一个静态变量中(在 MainActivity.java 中),现在我必须将这个 session ID 作为 cookie 值发送到我的其他网络服务。

我这样做过

public JSONObject getJSONQuestion(String url, List<NameValuePair> params) {

// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;

CookieStore cookieStore = new BasicCookieStore();
Cookie cookie = new BasicClientCookie("PHPSESSID",
MainActivity.SESSION_ID);

// MainActivity.SESSION_ID has my session id

cookieStore.addCookie(cookie);

HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet,
localContext);

HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}

// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}

// return JSON String
return jObj;
}

但我没有得到想要的响应。请查看我的 cookie 代码。我哪里做错了?

编辑

我明白了,

{"logout_success":1,"logout_reason":"no_activity_since_15_min"}

但我必须得到这个,

{


"random_questions":[
{
"questionid":"31",
"question":"I hate V-",
"answer1":"some answer 1",
"answer2":"another answer 2",
"answer3":"yet another answer 3",
"answer4":"a great answer 4",
"correct":"3"
},
… (more follow) …
]

}

编辑 2

这是我的 session 和cookie的php代码

<?php

$min = 15 * 60;
//$min = 60;

if (time() - $_SESSION['LAST_ACTIVITY'] > $min) {
session_destroy();
setcookie("PHPSESSID", "", time() - 3600, "/"); // delete session cookie
echo json_encode(array('logout_success' => 1, 'logout_reason' => 'no_activity_since_15_min'));
die;
} else {
$_SESSION['LAST_ACTIVITY'] = time();
mysqli_query($link, "UPDATE logged_in_users SET last_activity = " . $_SESSION['LAST_ACTIVITY'] . " WHERE session_id = '" . session_id() . "' ");
}

最佳答案

使用:来自 How to save session_id as cookie value and send it to server in android

DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);

List<Cookie> cookies = httpClient.getCookieStore().getCookies();
for (Cookie cookie : cookies) {
System.out.println("Cookie: " + cookie.toString());

if (cookie.getName().contains("PHPSESSID"))
guid = cookie.getValue();

}

HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

关于android - 通过 httpget 发送 cookie 值但没有得到所需的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16948068/

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