我在Windows Phone 8 PCL項目中工作。我正在使用第三方REST API,我需要使用由API發起的一些HttpOnly Cookie。看起來像從HttpClientHandler的CookieContainer獲取/訪問HttpOnly cookie是不可能的,除非您使用反射或其他後門。如何在Windows Phone 8中獲得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);
太棒了,正是我所需要的!謝謝。 – Nick 2014-04-13 15:27:37