Android 忽略了我的 HTTP POST 中的 cookie

Android is ignoring cookies in my HTTP POST

我的应用程序使用 HTTP POSTS,一般情况下该应用程序可以正常工作,但我在使用 cookie 时遇到了问题。我的代码似乎可以通过 HttpClient 变量获取 cookie,但 POST 的结果表明我的应用程序(作为客户端)没有记住任何东西。 (当我在我的电脑上使用 webbroswer 时,它工作正常,cookies 记住了我的名字) 此外,如果应用程序重新启动,cookie 就会消失(...但首先要注意的是)

这是我的代码:

static DefaultHttpClient httpclient;
static HttpPost httppost;
static CookieStore cookiestore;

new Thread(new Runnable() {
        public void run() {

            String URI_FOR_DOMAIN="http://chatbot.t.com/cgi-bin/elbot.cgi";

            httppost = new HttpPost(URI_FOR_DOMAIN);
            try {
                HttpResponse resp = httpclient.execute(httppost);
            } catch (IOException e) {
                e.printStackTrace();
            }

            cookiestore = ((DefaultHttpClient) httpclient).getCookieStore();
            List<Cookie> list = cookiestore.getCookies();
            app.logy("COOKIE STORE 1: " + cookiestore.toString());
            app.logy("COOKIE LIST  1: "+list.toString());
            for (int i=0;i<list.size();i++) {
                Cookie cookie = list.get(i);
                app.logy("COOKIE "+i+": "+cookie.toString());
            }

            HttpContext context = new BasicHttpContext();
            context.setAttribute(  ClientContext.COOKIE_STORE, cookiestore);

            httppost = new HttpPost(url);
            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<>(2);
                nameValuePairs.add(new BasicNameValuePair("ENTRY", userInput));
                nameValuePairs.add(new BasicNameValuePair("IDENT", userIDENT));
                nameValuePairs.add(new BasicNameValuePair("USERLOGID", userLOGID));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                app.logy("COOKIE STORE 2: "+cookiestore.toString());
                app.logy("COOKIE CONTEXT: " + context.toString());
                HttpResponse response = httpclient.execute(httppost, context);

                cookiestore = ((DefaultHttpClient) httpclient).getCookieStore();
                List<Cookie> list2 = cookiestore.getCookies();
                app.logy("COOKIE STORE 2: " + cookiestore.toString());
                app.logy("COOKIE LIST  2: "+list2.toString());
                for (int i=0;i<list2.size();i++) {
                    Cookie cookie = list2.get(i);
                    app.logy("COOKIE2 i="+i+": "+cookie.toString());
                }

                HttpEntity entity = response.getEntity();
}


                    }
                    app.logy(sb.toString());
                }
                catch (IOException e) { e.printStackTrace(); }
                catch (Exception e) { e.printStackTrace(); }


            } catch (ClientProtocolException e) {
                app.logy("HttpPOST (ClientProtocol) error: "+e);
            } catch (IOException e) {
                app.logy("HttpPOST (I/O) error: "+e);
            }

        }
    }).start();

这些值反映在上述代码的 Logcat 中。 cookie 似乎在那里,但是 HttpResponse response = httpclient.execute(httppost, context);似乎忽略了他们。

COOKIE STORE 2: [[version: 0][name: cookie_user_name][value: Albert][domain: elbot-e.artificial-solutions.com][path: /][expiry: Thu Oct 08 22:26:54 GMT+02:00 2015]]
COOKIE LIST  2: [[version: 0][name: cookie_user_name][value: Albert][domain: elbot-e.artificial-solutions.com][path: /][expiry: Thu Oct 08 22:26:54 GMT+02:00 2015]]
COOKIE2 i=0: [version: 0][name: cookie_user_name][value: Albert][domain: elbot-e.artificial-solutions.com][path: /][expiry: Thu Oct 08 22:26:54 GMT+02:00 2015]

首先,您应该对任何类型的 api 调用使用 AsyncTask。创建单独的线程是可行的,但这是一种不好的做法,而不是 android 方式。 其次,在您的 api 调用中使用 HttpUrlConnection 而不是 HTTPPost。这是进行 api 交互的新方法。 尝试这两种方法,看看您的 cookie 问题会怎样。

@Josh 编辑: 事实证明,@cj1090 向我发送了正确的建议,因此我将编辑他的答案以添加实现并将其标记为绿色。 按照他的建议,我改变了策略并实现了一个基于 HttpURLConnection 的 AsyncTask 而不是 Thread+HTTPpost 解决方案。该代码运行完美,cookie 由 HttpURLConnection 对象在内部处理。但是,如果应用程序被销毁,cookie 就会消失,但这是另一天的话题。 享受代码! BASE_URL 是一个经典的 HTML 网站 url,用户通过将字符串发送到 params[0]:

来与其通信
  //Call the AsyncTask like this:
final String BASE_URL = "http://elba.artisol.com/cgi/bot.cgi";
String fromUserTextbox="hello"
new elbotHttpTask().execute(url);


   public class elbotHttpTask extends AsyncTask<String, Void, Integer> {

        @Override
        protected Integer doInBackground(String... params) {
            InputStream inputStream = null;
            HttpURLConnection urlConnection = null;
            Integer result = 0;
            try {
                URL obj = new URL(BASE_URL);
                HttpURLConnection con = (HttpURLConnection) obj.openConnection();
                con.setRequestMethod("POST");
                con.setDoInput(true);
                con.setDoOutput(true);
                //con.setRequestProperty("User-Agent", USER_AGENT);

                app.userInput=params[0];

                List<NameValuePair> parameters = new ArrayList<NameValuePair>();
                parameters.add(new BasicNameValuePair("ENTRY", params[0]));
                parameters.add(new BasicNameValuePair("IDENT", app.sessionIDENT));
                parameters.add(new BasicNameValuePair("USERLOGID", app.sessionUSERLOGID));


                // For POST only - START
                app.logy("pp.getQuery(parameters): "+app.getQuery(parameters));
                OutputStream os = con.getOutputStream();
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(os, "UTF-8"));
                writer.write(app.getQuery(parameters));
                writer.flush();
                writer.close();
                os.close();

                con.connect();

                int responseCode = con.getResponseCode();
                System.out.println("POST Response Code :: " + responseCode);

                if (responseCode == HttpURLConnection.HTTP_OK) { //success
                    BufferedReader in = new BufferedReader(new InputStreamReader(
                            con.getInputStream()));
                    String inputLine;
                    StringBuffer response = new StringBuffer();

                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                    in.close();
                    consumeHTMLresponse(response.toString());
                    result = 1;
                } else {
                    System.out.println("POST request not worked");
                    result = 0;
                }
            } catch (Exception e) {
                app.logy(e.getLocalizedMessage());
            }
            return result; //"Failed to fetch data!";
        }



        @Override
        protected void onPostExecute(Integer result) {
            if(result == 1){
                app.logy("onPostEXECUTE SUCCESS");
            }else{
                app.logy("Failed to fetch data!");
            }
        }
    }