我在一个 Windows Phone 8 PCL 项目中工作。我正在使用第 3 方 REST API,我需要使用一些由 API 发起的 HttpOnly cookie。除非您使用反射或其他后门,否则似乎无法从 HttpClientHandler 的 CookieContainer 获取/访问 HttpOnly cookie。
我需要获取这些 cookie 并在后续请求中发送它们,否则我将无法使用此 API - 我该如何完成?这是我当前的请求代码的样子:
提前致谢。
//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);