簡體   English   中英

如何在Windows Phone 8中獲取HttpOnly cookie?

[英]How can I get HttpOnly cookies in Windows Phone 8?

我在Windows Phone 8 PCL項目中工作。 我正在使用第三方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);

HttpOnly cookie位於CookieContainer中,它只是不暴露。 如果您將該CookieContainer的相同實例設置為下一個請求,它將在那里設置隱藏的cookie(只要請求到cookie指定的同一站點)。

該解決方案將一直有效,直到您需要序列化和反序列化CookieContainer,因為您正在恢復狀態。 一旦你這樣做,你就會丟失隱藏在CookieContainer中的HttpOnly cookie。 因此,更永久的解決方案是直接為該請求使用套接字,將原始請求作為字符串讀取,提取cookie並將其設置為下一個請求。 這是在Windows Phone 8中使用套接字的代碼:

public async Task<string> Send(Uri requestUri, string request)
{
   var socket = new StreamSocket();
   var hostname = new HostName(requestUri.Host);
   await socket.ConnectAsync(hostname, requestUri.Port.ToString());

   var writer = new DataWriter(socket.OutputStream);
   writer.WriteString(request);
   await writer.StoreAsync();

   var reader = new DataReader(socket.InputStream) 
   { 
      InputStreamOptions = InputStreamOptions.Partial 
   };
   var count = await reader.LoadAsync(512);

    if (count > 0)
      return reader.ReadString(count);
    return null;
}

還有第二種可能性 - 手動瀏覽響應頭,抓取然后使用一堆自定義代碼解析Set-Cookie頭。

它看起來像這樣,當你要匹配並保存一個PHPSESSID cookie時(假設LatestResponse是包含網站響應的HttpResponseMessage ):

if (LatestResponse.Headers.ToString().IndexOf("Set-Cookie:") != -1) try
        {
            string sid = LatestResponse.Headers.ToString();
            sid = sid.Substring(sid.IndexOf("Set-Cookie:"), 128);
                if (sid.IndexOf("PHPSESSID=") != -1)
                {
                    settings.Values["SessionID"] = SessionID = sid.Substring(sid.IndexOf("PHPSESSID=") + 10, sid.IndexOf(';') - sid.IndexOf("PHPSESSID=") - 10);
                    handler.CookieContainer.Add(new Uri("http://example.com", UriKind.Absolute), new System.Net.Cookie("PHPSESSID", SessionID));
                }
        } catch (Exception e) { 
            // your exception handling
        }

請注意,除非手動刪除,否則此代碼CookieContainer Cookie插入CookieContainer以獲取該對象的生命周期。 如果要將其包含在新對象中,只需拉出正確的設置值並將其添加到新容器中即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM