在Android应用程序中的http响应中缺少cookie

时间:2014-07-06 17:01:00

标签: java android cookies httpclient

我正在尝试使用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

1 个答案:

答案 0 :(得分:1)

您必须访问请求中的标题。

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

特别注意以下事项:]

MimeType标头值

发布正文 - &#34; name&#34;,&#34;密码&#34;的表单值究竟发生了什么?并且是安卓的邮件正文,是webapp发送的内容。

验证webapp是POST而不是GET。

  

HttpPost postRequest = new HttpPost

上面的一行是设置一个默认的邮件标题集合,服务器正在从标题集的destops集合中处理非常DIFF。

比较相应的标题,通过在Android应用中添加/删除相应的标题来均衡它们,您将从nginx获得相同的结果....