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

Posted

技术标签:

【中文标题】通过 httpget 发送 cookie 值但没有得到所需的响应【英文标题】:Sending cookie value via httpget but not getting the desired response 【发布时间】:2013-06-01 15:12:35 【问题描述】:

我正在调用我的登录网络服务,如下所示

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"

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

我已经这样做了

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

这是我的会话和 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() . "' ");

【问题讨论】:

您收到的响应或错误是什么?您可以编辑您的问题并将其包括在内吗? @QuentinSwain 是的,只是一分钟 @QuentinSwain 看看我的编辑。我没有收到任何错误。 如果您将正确的会话值传输到服务器并且您收到响应。看起来服务器变得混乱了。我猜您的会话配置为根据您在响应中收到的消息在 15 分钟后超时。可能是服务器将您的会话提前超时? 我看不到 web 服务中的代码是什么样的。只是离开我可以从帖子中看到的信息。如果您没有收到 HTTP 错误响应并且从服务器获得某种形式的有效响应,则可能存在逻辑或配置问题。希望您能找到解决方案。 【参考方案1】:

使用那个:来自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();

【讨论】:

感谢您的回答。你是我的救命稻草 D:

以上是关于通过 httpget 发送 cookie 值但没有得到所需的响应的主要内容,如果未能解决你的问题,请参考以下文章

Android URLConnection发送Get请求 HttpGet封装

Next.js 中为啥没有通过 getServerSideProps 将 cookie 发送到服务器?

jquery textarea 设置值但 \n 不起作用

在 Combine 中,您如何创建一个发送一个值但从未完成的发布者?

Internet Explorer 9 AJAX 请求上没有会话 Cookie

Android HttpClient 和 Cookie