7

Hello guys I'm trying to send a cookie with http post in android

here's my code

webUrl = "XXXXXX";
        webView = (WebView) findViewById(R.id.webview);
        try
        {
            CookieStore cookieStore = new BasicCookieStore();
            Cookie cookie = new BasicClientCookie("session_id", "1234");
            cookieStore.addCookie(cookie);

            DefaultHttpClient httpclient = new DefaultHttpClient();

            BasicHttpContext mHttpContext = new BasicHttpContext();

            mHttpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

             HttpGet httpget = new HttpGet("XXXXX");

             HttpResponse response = httpclient.execute(httpget);
             HttpEntity entity = response.getEntity();

             Log.i("TAG", "Login form get: " + response.getStatusLine());
             if (entity != null) {
                 entity.consumeContent();
             }
             Log.i("TAG", "Initial set of cookies:");
             List<Cookie> cookies = httpclient.getCookieStore().getCookies();
             if (cookies.isEmpty()) {
                 Log.i("TAG", "None");
             } else {
                 for (int i = 0; i < cookies.size(); i++) {
                     Log.i("TAG", "- " + cookies.get(i).toString());
                 }
             }

            HttpPost httpost = new HttpPost("XXXXX");

            List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair("session_id", "1234"));
            nvps.add(new BasicNameValuePair("selected_cid", "1234"));
            nvps.add(new BasicNameValuePair("selected_kaisai_id", "1234"));
            nvps.add(new BasicNameValuePair("iid", "1234"));
            nvps.add(new BasicNameValuePair("tid", "1234"));

            httpost.setEntity(new UrlEncodedFormEntity(nvps));

            response = httpclient.execute(httpost);
            entity = response.getEntity();

            Log.i("TAG", "Login form get: " + response.getStatusLine());
            if (entity != null) {
                entity.consumeContent();
            }

            Log.i("TAG", "Post logon cookies:");
            cookies = httpclient.getCookieStore().getCookies();
            if (cookies.isEmpty()) {
                System.out.println("None");
            } else {
                for (int i = 0; i < cookies.size(); i++) {
                    Log.i("TAG", "- " + cookies.get(i).toString());
                }
            }
            httpclient.getConnectionManager().shutdown();        
        }
        catch(Exception e)
        {
            Log.i("TAG", e.getMessage());
        }
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        String postData = "session_id=1234";
        webView.postUrl(webUrl, EncodingUtils.getBytes(postData, "BASE64"));

I can post the data from postData but not the cookie the cookie isn't showed in the webview here's the PHP side

public function cookietestAction()
    {
        echo "mixi for netrecorder tester<br/>\n";

        echo "<br/>Cookies...<br/>\n\n";
        $request = new Zend_Controller_Request_Http();
        $sessionid = $request->getCookie('session_id');
        echo "<br/>\nsessionid = ".$sessionid."<br/>\n";

        $selected_cid = $request->getCookie('selected_cid');
        echo "\nselected_cid = ".$selected_cid."<br/>\n";

        $selected_kaisai_id = $request->getCookie('selected_kaisai_id');
        echo "\nselected_kaisai_id = ".$selected_kaisai_id."<br/>\n";

        $iid = $request->getCookie('iid');
        echo "\niid = ".$iid."<br/>\n";

        $tid = $request->getCookie('tid');
        echo "\ntid = ".$tid."<br/>\n";

        if($this->getRequest()->isPost())
        {
            echo "<br/>POST data only...<br/>\n\n";
            $post = $this->getRequest()->getPost('session_id');
            echo "<br/>\nsession_id=".$post."<br/>\n";
            $post = $this->getRequest()->getPost('selected_cid');
            echo "selected_cid=".$post."<br/>\n";
            $post = $this->getRequest()->getPost('selected_kaisai_id');
            echo "selected_kaisai_id=".$post."<br/>\n";
            $post = $this->getRequest()->getPost('tid');
            echo "tid=".$post."<br/>\n";
            $post = $this->getRequest()->getPost('iid');
            echo "iid=".$post."<br/>\n";
        }
}

can anyone tell me what's wrong? thank you

Niko Adrianus Yuwono
  • 11,012
  • 8
  • 42
  • 64

1 Answers1

19

to add cokie use it like this: it should be added in header

        post.addHeader("Cookie", " PHPSESSID="+PHPSESSID+"; gc_userid="+gc_user+"; gc_session="+gc);

for you it should be like:

     httpost.addHeader("Cookie","session_id = 1234;"...)
Athul Harikumar
  • 2,501
  • 18
  • 16
  • sorry droidhot,but it didn't work,do I need to add more code? thanks – Niko Adrianus Yuwono Sep 05 '12 at 11:40
  • 1
    Now, *that* looks better. Your initial code was just setting a header - not a *cookie* header. – Andrew Barber Sep 05 '12 at 11:42
  • yap actually my server wanted both so i misplaced it with the other – Athul Harikumar Sep 05 '12 at 11:43
  • I'm sorry still not working,is there anything wrong with my code? it's working in the iphone but in the android it didn't work,but the logic is different – Niko Adrianus Yuwono Sep 05 '12 at 11:49
  • this has to be changed as per your server " PHPSESSID="+PHPSESSID+"; gc_userid="+gc_user+"; gc_session="+gc – Athul Harikumar Sep 05 '12 at 11:50
  • Yes I've changed it into httpost.addHeader("Cookie","session_id = 1234;") but i didn't send all the cookie value because this is only for testing,maybe because of that?I'll try it later and inform you,thanks for your help – Niko Adrianus Yuwono Sep 05 '12 at 17:30
  • 3
    I'm finally found a solution using this CookieSyncManager.createInstance(this); CookieManager cookieManager = CookieManager.getInstance(); String getcookie = cookieManager.getCookie(webUrl); cookieManager.setCookie(webUrl, "session_id="+id); cookieManager.setCookie(webUrl, "selected_cid="+id); cookieManager.setCookie(webUrl, "selected_kaisai_id="+id); cookieManager.setCookie(webUrl, "iid="+id); cookieManager.setCookie(webUrl, "tid="+id); but I'll mark @droidhot answer OK,thanks for the help droidhot! – Niko Adrianus Yuwono Sep 11 '12 at 08:02
  • @nayoso: Why did you do a `getCookie` in that? – Ε Г И І И О Jul 11 '14 at 03:52