Session cookie not used

After many, many hours, I managed to figure out how to share cookies between my client and my webview. My problem right now is that for some reason my session file is not being used.

In android docs I found: public void setCookie (String url, String value) Because: API level 1 Set a cookie for the given URL. An old cookie with the same name / name / host will be deleted. A new cookie will be added if it has not expired or does not have an expiration date, which means that it is a session cookie.

A thing is a general cookie with a set expiration date and it works. Does anyone have any idea why my session cookie is not used or if it really is because setCookie cannot do this, how can I do it differently.

Here is my code:

    package mds.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Home extends Activity {



    public static final String LOG_TAG = "Droidnova";

    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    private String tmDevice;
    private String sid;
    private String url;
    public static Cookie cookie = null;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        CookieSyncManager.createInstance(this);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);

        final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
        tmDevice = "blabla" + tm.getDeviceId();

        postData();

        url = "mywebsite="+sid.substring(5); 

        Log.d(LOG_TAG, "cookie value: " + cookie);

        if (cookie != null) {
            cookieManager.removeSessionCookie();
            String cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();
            cookieManager.setCookie(cookie.getDomain(), cookieString);
            CookieSyncManager.getInstance().sync();
        }

        setContentView(R.layout.web);
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new HelloWebViewClient());
        myWebView.loadUrl(url);
    }

    public void postData() {
        // Create a new HttpClient and Post Header
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("my website");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("uid", tmDevice));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

            inputStreamToString(response.getEntity().getContent());


            List<Cookie> cookies = httpclient.getCookieStore().getCookies();
            if (!cookies.isEmpty()) {
                for (int i = 0; i < cookies.size(); i++) {
                    cookie = cookies.get(i);
                }
            }

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }

   }

    private void inputStreamToString(InputStream is) {
        String line = "";
        StringBuilder total = new StringBuilder();

        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        // Read response until the end
        try {
            while ((line = rd.readLine()) != null) { 
                total.append(line); 
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        sid = total.toString();         
    }

}
+3
source share
1 answer

Please note that this is not a solution, this is a workaround if you have access to the page code.

Instead of passing a cookie to the webView, you can pass the POST value of the cookie:

String postData = cookie.getName() + "=" + cookie.getValue();
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new HelloWebViewClient());
myWebView.postUrl(loadUrl,EncodingUtils.getBytes(postData, "BASE64") );

On the website in the authorization engine, you must check the POST data as well as the session.

+1
source

All Articles