HTTPDefaultClient:无法获得Set-Cookie标头

时间:2011-11-29 11:31:08

标签: android cookies setcookie

我们正在使用Android API并使用HTTPDefaultClient发布,当我们收到标题时,我们没有得到我们需要的“Set-Cookie”标题 - 但是当我们使用原始套接字尝试相同的URL时,我们得到它

网址正在从我们的Android应用中收到正确的信息,但我们似乎无法从中检索此标头。

有人可以帮忙吗?它驱使我们便盆似乎有很多指南可以自己设置一个cookie,但我们找不到任何东西来找回它

 try {
      HttpClient client = new DefaultHttpClient();
      DefaultHttpClient defaultClient = new DefaultHttpClient();
      HttpGet request = new HttpGet();
      request.setURI(new URI(loginUrl));
      HttpResponse response = client.execute(request);

      Header[] headers = response.getAllHeaders();

      System.out.println("-----------------------------------");

      for(int i = 0; i < headers.length; i++) {
      Header header = headers[i];
      Log.i("HeaderName", "Header name: " + header.getName());
      Log.i("HeaderValue", "Header value: " + header.getValue());
      }
 }
 catch(Exception e) {e.printStackTrace();}

1 个答案:

答案 0 :(得分:0)

您的代码与我完美配合。

尝试使用http://www.google.it

            try {
                  HttpClient client = new DefaultHttpClient();
                  //DefaultHttpClient defaultClient = new DefaultHttpClient();
                  HttpGet request = new HttpGet();
                  request.setURI(new URI("http://www.google.it"));
                  HttpResponse response = client.execute(request);

                  Header[] headers = response.getAllHeaders();

                  //System.out.println("-----------------------------------");

                  for(int i = 0; i < headers.length; i++) {
                  Header header = headers[i];
                  Log.i("HeaderName", "Header name: " + header.getName());
                  Log.i("HeaderValue", "Header value: " + header.getValue());
                  }
             }
             catch(Exception e) {e.printStackTrace();}

输出:

11-29 15:23:23.630: I/HeaderName(2269): Header name: Date
11-29 15:23:23.630: I/HeaderValue(2269): Header value: Tue, 29 Nov 2011 14:23:23 GMT
11-29 15:23:23.630: I/HeaderName(2269): Header name: Expires
11-29 15:23:23.630: I/HeaderValue(2269): Header value: -1
11-29 15:23:23.630: I/HeaderName(2269): Header name: Cache-Control
11-29 15:23:23.630: I/HeaderValue(2269): Header value: private, max-age=0
11-29 15:23:23.630: I/HeaderName(2269): Header name: Content-Type
11-29 15:23:23.630: I/HeaderValue(2269): Header value: text/html; charset=ISO-8859-1
11-29 15:23:23.630: I/HeaderName(2269): Header name: Set-Cookie
11-29 15:23:23.630: I/HeaderValue(2269): Header value: PREF=ID=125f8d281270e018:FF=0:TM=1322576603:LM=1322576603:S=gPXe6FZbpDuKjcUR; expires=Thu, 28-Nov-2013 14:23:23 GMT; path=/; domain=.google.it
11-29 15:23:23.630: I/HeaderName(2269): Header name: Set-Cookie
11-29 15:23:23.630: I/HeaderValue(2269): Header value: NID=53=hnqogpxyCO2Z4H7khIJtp_5Qlx-qewYq1zso-dAXxdZlYXMY6vQ0Gx01HTz8waYz0ICeJ_roBYRmiD_85PaGL6spuu-c1LP52_qxgvx8sEvMrow1M6Qd2mtgUVIUuD2P; expires=Wed, 30-May-2012 14:23:23 GMT; path=/; domain=.google.it; HttpOnly
11-29 15:23:23.630: I/HeaderName(2269): Header name: Server
11-29 15:23:23.630: I/HeaderValue(2269): Header value: gws
11-29 15:23:23.630: I/HeaderName(2269): Header name: X-XSS-Protection
11-29 15:23:23.630: I/HeaderValue(2269): Header value: 1; mode=block
11-29 15:23:23.630: I/HeaderName(2269): Header name: X-Frame-Options
11-29 15:23:23.630: I/HeaderValue(2269): Header value: SAMEORIGIN
11-29 15:23:23.630: I/HeaderName(2269): Header name: Transfer-Encoding
11-29 15:23:23.630: I/HeaderValue(2269): Header value: chunked

您确定收到的响应应包含Set-Cookie标头吗?

编辑:您是否已将互联网权限添加到AndroidManifest.xml文件中?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yourpackage.app" ...>
    <uses-sdk android:minSdkVersion="##" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application ...>
        ...
    </application>
</manifest>