简体   繁体   English

使用来自Webview的cookie执行HTTP请求

[英]Perform HTTP requests using cookies from webview

I have this scenario which my app shows in a webView a 2-page login process. 我有这种情况,我的应用程序会在webView中显示一个2页的登录过程。

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. 我正在尝试在webView中执行登录,然后执行来自本机代码的请求。 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. 我意识到我需要从webView获取存储的cookie(但是从哪个URL?从第一页还是第二个URL?),然后将cookie用于本机代码请求。

Can someone please tell me how to go about it? 有人可以告诉我该怎么做吗? The login process is easy - the user logs in through the webview - fine. 登录过程很简单-用户通过Webview登录-很好。 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? 现在,我知道如何使用cookie管理,但是我不知道我应该寻找哪个cookie-它是第一个登录页面的URL吗? 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? 接下来,如何使用Cookie将GET请求发送回服务器,以便服务器知道我已通过身份验证?

I appreciate the answers I'm clueless and begging for help :) 我感谢我无能为力的乞求的答案:)

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();

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. 我做了相反的事情:我使用loopj Android异步Http客户端登录,并希望将会话cookie应用于同一网站的webview。 I don't know if it will help you, but, I am going to post my code for copying over the cookies. 我不知道这是否对您有帮助,但是,我将发布用于复制Cookie的代码。 Maybe seeing the process will help you to look for the items you need... to copy cookies from webview, to HTTP. 也许看到该过程将帮助您查找所需的项目...以将cookie从Webview复制到HTTP。 I can't offer further help, since I'm fairly new at Android. 由于我是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: onCreate()初始化:

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

After HTTP login: HTTP登录后:

// 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. 请注意,loopj类似于webview,因为所有cookie管理和发送都是自动的。 I just copy all cookies for the domain. 我只复制该域的所有cookie。 I think you'd be fine, doing the same... thus, no worry about whether from the first or second page. 我认为您也可以这样做,因此...不必担心从第一页还是第二页开始。

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. 用户通过Web视图登录后,便会在设备上设置Cookie。 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. 稍后,一旦我想对服务执行本机api调用,我会向cookie管理器询问基于URL设置的cookie。

I then take the important header that is used to authenticate on the server and send it along with my api calls. 然后,我获取用于在服务器上进行身份验证的重要标头,并将其与我的api调用一起发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM