我正在一个Windows Phone 8 PCL项目中工作。我正在使用第三方REST API,并需要使用由该API发出的一些HttpOnly cookie。似乎无法从HttpClientHandler的CookieContainer获取/访问HttpOnly cookie,除非您使用反射或其他后门。
我需要获取这些cookie并在随后的请求中发送它们,否则我将无法使用此API - 我该怎么做?以下是我的当前请求代码:
提前感谢您的帮助。
我需要获取这些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);