【问题标题】:Missing cookie in http response in android appandroid应用程序中的http响应中缺少cookie
【发布时间】:2014-08-27 04:54:03
【问题描述】:

我正在尝试使用来自 android 应用程序的 apache HttpClient 向服务器发出请求,但我得到的响应没有标头 Set-Cookies。 我尝试在桌面应用程序上做同样的事情并且它有效,我收到了所有标题。 我究竟做错了什么?帮帮我,请解决这个问题。

HttpClient httpclient1 = new DefaultHttpClient();
        HttpGet req1 = new HttpGet("http://www.example.com");
        req1.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        req1.addHeader("Accept-Encoding", "gzip,deflate,sdch");
        req1.addHeader("Accept-Language", "ru,en-US;q=0.8,en;q=0.6,uk;q=0.4");
        req1.addHeader("Connection", "keep-alive");
        req1.addHeader("Host", "www.example.com");
        req1.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36");
        String cookie = "";
        try {
            HttpResponse response1 = httpclient1.execute(req1);
            Header[] headers = response1.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                System.out.println(headers[i] + "");
                if (headers[i].toString().startsWith("Set-Cookie: "))
                    cookie = headers[i].toString().substring(12);
            }   

            HttpClient httpclient = new DefaultHttpClient();

            HttpPost postRequest = new HttpPost("http://www.example.com/1.php");
            postRequest.addHeader("Referer", "http://www.example.com/2.php");
            if (cookie.length() > 0) {
                cookie = cookie.replace(" path=/", "");
                postRequest.addHeader("Cookie", cookie);
            }

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("username", "my-username"));
            nameValuePairs.add(new BasicNameValuePair("password", "my-password"));
            postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(postRequest);
            Header[] hs = postRequest.getAllHeaders();
            for (int i = 0; i < hs.length; i++) {
                System.out.println(hs[i] + "");
            }
            System.out.println(response.getStatusLine().getStatusCode() + "");
            Header[] hs3 = response.getAllHeaders();
            for (int i = 0; i < hs3.length; i++) {
                System.out.println(hs3[i] + "");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            System.err.println("ClientProtocolException");
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("IOException");
        }

这是我使用 android 应用收到的标题:

200
Server: nginx/0.7.67
Date: Sun, 06 Jul 2014 16:25:26 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.3.3-7+squeeze14
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Vary: Accept-Encoding

这是来自桌面应用程序:

302
Server: nginx/0.7.67
Date: Sun, 06 Jul 2014 16:39:55 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.3.3-7+squeeze14
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: http://www.example.com/
Set-Cookie: uid=12345; expires=Tue, 19-Jan-2038 03:14:07 GMT; path=/
Set-Cookie: pass=11111111111111111111111111111111; expires=Tue, 19-Jan-2038 03:14:07 GMT; path=/
Vary: Accept-Encoding
Content-Length: 139

【问题讨论】:

  • 不确定是否有人有同样的原因,但是当我尝试通过开放的 WiFi 网络使用应用程序时遇到了这个问题,我想这个网络从原始响应中剪切了 Set-Cookie 标头。当我通过移动网络连接时,一切正常。

标签: java android cookies httpclient


【解决方案1】:

您必须能够访问请求中的标头。

比较它们......它们不会是一样的......

请特别注意以下几点:]

MimeType 标头值

帖子正文 - “名称”、“密码”的表单值究竟会发生什么,并且是 android 的帖子正文与 webapp 发送的内容完全相同。

验证 webapp 是 POST 而不是 GET。

HttpPost postRequest = 新的HttpPost

上面的行设置了一组默认的 Post 标头,您的服务器正在处理的标头与 destops 标头集合非常不同。

比较各自的标头,通过在您的 android 应用程序中添加/删除适当的标头来平衡它们,您将从 nginx 获得相同的结果......

【讨论】:

  • 我在两个应用程序中使用的有问题的代码片段,但我不知道为什么它会给出不同的结果。也许它与 Android 操作系统的细节有关,以及它如何处理 cookie?
  • 您实际上没有使用相同的代码。 Webapp 使用浏览器并从浏览器获取 http 堆栈。浏览器设置它想要的任何标题。另一方面,Android 使用 httpclient(完全 DIFF 堆栈)获得一组完全 DIFF 的默认标头。在每种情况下,如果不更多地参与 EXACT 标头,您将无法解决您的问题。 stackoverflow.com/questions/23291209/…
  • 那么,你的意思是,非android应用程序中的apache HttpClient方法addHeader()没有设置标题?
  • 确实如此。但是,我的意思是'new http post()'可能已经默认设置了一个标题,你可能不知道。除非您显示所有标题。在添加标题之前,您可能需要删除标题。
  • 删除标题没有帮助,但我在这里找到了解决方案 stackoverflow.com/questions/21432738/…
猜你喜欢
  • 2014-05-27
  • 1970-01-01
  • 2017-06-16
  • 1970-01-01
  • 2020-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多