Cordova:与本机插件共享 webview cookie

Cordova: Share webview cookies with native plugins

我有一个 cordova 应用,运行 iOS 和 Android,它使用 cookie 进行身份验证(用户只需输入电子邮件和密码,应用内浏览器设置cookies,后端在 Rails 中设计)。这到目前为止有效。

现在我有一个 android 和 iOS 的原生插件,它以后台模式 (iOS) / 作为服务 (Android) 上传一些数据。问题是,插件试图访问的 URL 也受到保护。因此插件需要以某种方式将 cookie 与请求一起发送。

我找到了很多文章,其中大部分都有点陈旧(比如这篇http://blog.winfieldpeterson.com/2013/01/17/cookies-in-hybrid-android-apps),每一个解决方案都感觉有点矫枉过正。

我正在使用 Cordova 4.3.0,也许有一个标准的解决方案,我还没有找到(并且在所有其他文章都写完后得到了更新)。

这是我的 Android 和 iOS 代码的一部分,它将数据发送到服务器:

HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
HttpClient httpclient = new DefaultHttpClient();

SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
SingleClientConnManager mgr = new SingleClientConnManager(httpclient.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, httpclient.getParams());

// Set verifier
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

HttpPost httppost = new HttpPost(locationCache.getUrl());
httppost.setHeader("Content-type", "application/json");

JSONObject body;
body = new JSONObject();
body.put("someKey", "someData");

HttpResponse response = httpclient.execute(httppost);

和 iOS 版本:

NSError * err;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:geodata options:0 error:&err];
NSString * jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:url]];
postRequest.HTTPMethod = @"POST";
[postRequest setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
postRequest.HTTPBody = [jsonString dataUsingEncoding: NSUTF8StringEncoding];
[NSURLConnection sendAsynchronousRequest:postRequest queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                           if(error) {
                             NSLog(@"Httperror: %@, %d", error.localizedDescription, error.code);
                           } else {
                              NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
                              NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                              NSLog(@"REQUEST SENT TO SERVER! HTTP response code: %d; response body: %@", responseCode, responseString);
                           }
                       }];

希望有人能提供帮助。

在 iOS 上,您可以获得 url 的 cookie,如下所示:

NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"__YOUR_URL__"]];

或所有这样的 cookie:

NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];

之后,你可以这样设置请求头:

NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
[postRequest setAllHTTPHeaderFields:headers]; 

在 android 上,您有网络视图 CookieManager

CookieManager cookieManager=CookieManager.getInstance();
String cookie=cookieManager.getCookie(url);