从Windows Phone 8 / PCL访问HTTPOnly cookie [英] Accessing HTTPOnly cookies from Windows Phone 8/PCL
问题描述
我在Windows Phone 8 PCL项目中工作。 我正在使用第三方REST API,我需要使用由API发起的一些HttpOnly cookie。 似乎从HttpClientHandler的CookieContainer获取/访问HttpOnly cookie不是
,除非你使用反射或其他后门。
I am working in a Windows Phone 8 PCL project. I am using a 3rd party REST API and I need to use a few HttpOnly cookies originated by the API. It seems like getting/accessing the HttpOnly cookies from HttpClientHandler's CookieContainer is not possible unless you use reflection or some other backdoor.
我需要获取这些cookie并在随后发送它们请求否则我将无法使用此API - 我该如何完成此操作? 以下是我当前的请求代码:
I need to get these cookies and send them in subsequent requests otherwise I am not going to be able to work with this API - how can I accomplish this? Here is what my current request code looks like:
提前致谢。
//Some request
HttpRequestMessage request = new HttpRequestMessage();
HttpClientHandler handler = new HttpClientHandler();
//Cycle through the cookie store and add existing cookies for the susbsequent request
foreach (KeyValuePair<string, Cookie> cookie in CookieManager.Instance.Cookies)
{
handler.CookieContainer.Add(request.RequestUri, new Cookie(cookie.Value.Name, cookie.Value.Value));
}
//Send the request asynchronously
HttpResponseMessage response = await httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
//Parse all returned cookies and place in cookie store
foreach (Cookie clientcookie in handler.CookieContainer.GetCookies(request.RequestUri))
{
if (!CookieManager.Instance.Cookies.ContainsKey(clientcookie.Name))
CookieManager.Instance.Cookies.Add(clientcookie.Name, clientcookie);
else
CookieManager.Instance.Cookies[clientcookie.Name] = clientcookie;
}
HttpClient httpClient = new HttpClient(handler);
推荐答案
您不应该这样做,因为您无法看到这些Cookie。您需要跨多个客户端/请求重用cookie容器。请查看以下内容:如何
to:获取和设置Cookie 。
也许这会有所帮助。我必须为两个不同的应用程序使用相同的技术。
Maybe that will help. I had to use the same technique already for two different apps.
这篇关于从Windows Phone 8 / PCL访问HTTPOnly cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!