【发布时间】:2014-06-25 14:22:35
【问题描述】:
我正在尝试弄清楚如何清除 windows phone 8 cordova 插件中的 cookie。
我一直在使用http://cloudstore.blogspot.in/2010/09/clearing-cookies-on-windows-phone-7-or.html 和http://developer.nokia.com/community/wiki/Integrate_Facebook_to_Your_Windows_Phone_Application 的建议
基本上你要么获取网络浏览器实例并使用方便的方法,如下所示:
await browserInstance.ClearCookiesAsync();
或者您从请求中获取 cookie 并丢弃/过期它们,如下所示:
HttpWebRequest _webRequest;
Uri uri = new Uri("https://blah.com");
CookieContainer _cookieContainer = new CookieContainer();
_webRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
_webRequest.CookieContainer = this._cookieContainer;
var cookies = this._cookieContainer.GetCookies(uri);
foreach (Cookie cookie in cookies)
{
cookie.Discard = true;
cookie.Expired = true;
}
我的问题有两个:
1) 我不知道如何获取cordova浏览器实例以便尝试调用ClearCookiesAsync()方法
2) 我没有使用 HTTPWebRequest 对象,因为我需要访问设置一些标头信息,我使用的是 HTTPClient 对象和 HTTPRequestMessage,即:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://blah.com");
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/html"));
client.DefaultRequestHeaders.Add("Some custom stuff","More custom stuff");
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get,"/something/hello.php");
HttpResponseMessage response = await client.SendAsync(req);
response.EnsureSuccessStatusCode();
string responseBody = string.Empty;
responseBody = await response.Content.ReadAsStringAsync();
Debug.WriteLine(responseBody);
所以基本上,如果我能弄清楚如何在插件中从 cordova 获取浏览器实例,或者从 HTTPClient 中清除 cookie,就可以了。
编辑:有趣...How can I get HttpOnly cookies in Windows Phone 8?
显然你可以通过 HttpClientHandler 访问 cookie,所以我上面代码的开头是这样的......
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = new CookieContainer();
HttpClient client = new HttpClient(handler);
...但是创建一个新的 Cookie 容器似乎并没有清除 cookie?
这看起来也很有趣:Struggling trying to get cookie out of response with HttpClient in .net 4.5
我玩过上面的链接,它确实从 HttpResponseMessage 中检索了 cookie,但尝试使用 cookie.Discard 和 cookie.Expired 使这些 cookie 无效。
似乎答案是您必须让科尔多瓦浏览器实例丢弃 cookie,以上内容只会让您以似乎是只读的方式使用单个响应中的 cookie :(
类似/重复的问题:
Access to cookies or WebBroswer class from phonegap plugin running windows phone ClearCookiesAsync() on Cordova
【问题讨论】:
标签: cordova cookies windows-phone-8 plugins