android - 无法从 header 响应中获取 Cookie

标签 android cookies

我是初学者,我发出了一个 HTTP 请求并在这段代码中得到响应:

 public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.168.1.105/moodle/login/index.php"); //http://192.168.1.105/moodle/login/index.php

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", "admin"));
        nameValuePairs.add(new BasicNameValuePair("password", "dtkTj29000g!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        httppost.setHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        String responseContent = EntityUtils.toString(response.getEntity());
        Log.i("Response", responseContent );


        Header[] header=response.getAllHeaders();
        for(int i=0;i<header.length;i++){
            String a=header[i].getValue();
            String b=header[i].getName();
            Log.i("quangggggggggggggg",b+"__________"+a);
        }

        WebView webview = (WebView)findViewById(R.id.webkit1);
        webview.loadDataWithBaseURL(null,responseContent, "text/html", "utf-8",null);



    /*    cookies = mCookieStore.getCookies();
        for(int i=0;i<cookies.size();i++){
            String a=cookies.get(i).toString();
            Log.i("quangggggggggggg",a);
        }*/



    } catch (ClientProtocolException e) { 

    } catch (IOException e) {

    } 

} 

我想要的一切都只是从 header 中获取 cookie,但返回的 header 没有 cookie。

我的 logcat 中有这个:

Date__________Mon, 14 Oct 2013 08:05:09 GMT
Server__________Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.16
X-Powered-By__________PHP/5.4.16
Set-Cookie__________MoodleSession=tnd2sbd71qr1ft7s0jjskf3qk5; path=/moodle/
Expires__________
Cache-Control__________private, pre-check=0, post-check=0, max-age=0
Pragma__________no-cache
Content-Language__________en
Content-Script-Type__________text/javascript
Content-Style-Type__________text/css
X-UA-Compatible__________IE=edge
Accept-Ranges__________none
X-Frame-Options__________sameorigin
Keep-Alive__________timeout=5, max=100
Connection__________Keep-Alive
Transfer-Encoding__________chunked
Content-Type__________text/html; charset=utf-8

但是我在网络浏览器中看到的响应头是:

Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Language:en
Content-Length:586
Content-Type:text/html
Date:Mon, 14 Oct 2013 08:12:55 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=97
Location:http://192.168.1.105/moodle/login/index.php?testsession=2
Pragma:no-cache
Server:Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.16
Set-Cookie:MoodleSession=ol6icib1pv4vv0qlpk9ng1nbn3; path=/moodle/
Set-Cookie:MOODLEID1_=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/moodle/
X-Powered-By:PHP/5.4.16

我不知道为什么我不能得到cookie,你能帮帮我吗? 非常感谢!

最佳答案

您的 header 确实包含一个 Cookie,即 Set-Cookie。像这样取回

Header[] cookieHeader = httpResponse.getHeaders("Set-Cookie");
        if (cookieHeader.length > 0) {
            HttpUtil.PHPSESSID = cookieHeader[0].getValue();
        }

关于android - 无法从 header 响应中获取 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19357505/

相关文章:

java - 在后台检查互联网

java - 运行 Genymotion 时出现白屏

javascript - 如何渲染JS为cookie生成指纹?

java - 访问 REST Web 服务的摘要式身份验证

c# - 为什么在 Request.Cookies[FormsAuthentication.FormsCookieName] 中找不到 ASPXAUTH cookie?

c# - gecko 清除缓存历史记录和 cookie

android - 通过Android使用ClientLogin直接上传YouTube

java - 在运行时获取 RelativeLayout 的尺寸

android - 从 Android 到自定义套接字服务器的 SSL/TLS 连接

PHP/MYSQL - 将表字段保存为单独的字符串变量