Perform HTTP requests using cookies from webview

妖精的绣舞 提交于 2019-12-08 05:40:22

问题


I have this scenario which my app shows in a webView a 2-page login process.

The first page asks only to which domain to you plan on connecting. The second page asks for the credentials.

I'm trying to perform the login in the webView and then execute requests from my native code. I realize I need to get the stored cookie from the webView (but from which url? from the first page or the second one?), and then use the cookie for the native code requests.

Can someone please tell me how to go about it? The login process is easy - the user logs in through the webview - fine. Now, I know how to use the cookie manage but I dont know which cookie am I suppose to look for - is it the url of the first login page? is it the second one? does it matter?

Next, how do I use the cookie to send back to server with a GET request so the server will know I'm authenticated?

I appreciate the answers I'm clueless and begging for help :)


回答1:


Since the accepted answer does not really describe how it is done:

Put these lines somewhere where your app starts:

    CookieHandler.setDefault(new CookieManager()); // Apparently for some folks this line works already, for me on Android 17 it does not.
    CookieSyncManager.createInstance(yourContext); // or app will crash when requesting cookie

And then in your connection:

       String cookies = CookieManager.getInstance().getCookie(urlString);
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
      //  conn.setRequestMethod("GET");
      //  conn.setDoInput(true);
        if (cookies != null)
            conn.setRequestProperty("Cookie", cookies);
        // Starts the query
        conn.connect();



回答2:


I have done the opposite of you: I log in with loopj Android Asynchronous Http Client, and want the session cookies to apply to a webview, for the same website. I don't know if it will help you, but, I am going to post my code for copying over the cookies. Maybe seeing the process will help you to look for the items you need... to copy cookies from webview, to HTTP. I can't offer further help, since I'm fairly new at Android. (And, of course, I adapted my code from other people's posts.)

Class variable declarations:

private AsyncHttpClient loopjClient = new AsyncHttpClient();
private PersistentCookieStore myCookieStore;

onCreate() initialization:

myCookieStore = new PersistentCookieStore(this);
loopjClient.setCookieStore(myCookieStore);

After HTTP login:

// get cookies from the generic http session, and copy them to the webview
CookieSyncManager.createInstance(getActivity().getApplicationContext());
CookieManager.getInstance().removeAllCookie();
CookieManager cookieManager = CookieManager.getInstance();

List<Cookie> cookies = myCookieStore.getCookies();
for (Cookie eachCookie : cookies) {
    String cookieString = eachCookie.getName() + "=" + eachCookie.getValue();
    cookieManager.setCookie("http://www.example.com", cookieString);
    //System.err.println(">>>>> " + "cookie: " + cookieString);
}
CookieSyncManager.getInstance().sync();
// holy ****, it worked; I am automatically logged in for the webview session

Note that loopj is like the webview, in that all cookie management and sending are automatic. I just copy all cookies for the domain. I think you'd be fine, doing the same... thus, no worry about whether from the first or second page.




回答3:


At the end I found my way and it was pretty simple.

Once the user logs in through the webview - a cookie is set on the device. Later on once I want to perform Native api calls on the service I ask the cookie manager for the cookie that was set based on the url.

I then take the important header that is used to authenticate on the server and send it along with my api calls.



来源:https://stackoverflow.com/questions/27939856/perform-http-requests-using-cookies-from-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!