如何在Windows Phone 8中获取HttpOnly Cookie? [英] How can I get HttpOnly cookies in Windows Phone 8?

查看:139
本文介绍了如何在Windows Phone 8中获取HttpOnly Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows Phone 8 PCL项目中工作。我使用第三方REST API,我需要使用一些由API发起的HttpOnly Cookie。除非你使用反射或其他一些后门好像从HttpClientHandler的的CookieContainer获取/访问的HttpOnly饼干是不可能的。



我需要得到这些cookie,并在随后给他们请求否则我不能使用这个API - 我怎么能完成这个?这是我当前的请求代码看起来像:



提前感谢。

  //一些请求
HttpRequestMessage request = new HttpRequestMessage();
HttpClientHandler handler = new HttpClientHandler();

//通过cookie存储周期和增加现有的cookie的susbsequent请求
的foreach(KeyValuePair<字符串,饼干>饼干在CookieManager.Instance.Cookies)
{
handler.CookieContainer.Add(request.RequestUri,new Cookie(cookie.Value.Name,cookie.Value.Value));
}

//异步发送请求
HttpResponseMessage response = await httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();

//解析(在handler.CookieContainer.GetCookies曲奇clientcookie(request.RequestUri))cookie存储
的foreach所有返回的cookie和地点
{
如果( !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。因此,更永久的解决方案将直接使用Sockets作为该请求,将读取的原始请求作为字符串,提取cookie并将其设置为下一个请求。下面是在Windows Phone 8中使用Sockets的代码:

  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;
}


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.

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:

Thanks in advance.

//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);

解决方案

The HttpOnly cookie is inside the CookieContainer, it's only that is not exposed. If you set the same instance of that CookieContainer to the next request it will set the hidden cookie there (as long as the request is made to the same site the cookie specifies).

That solution will work until you need to serialize and deserialize the CookieContainer because you are restoring state. Once you do that you lose the HttpOnly cookies hidden inside the CookieContainer. So, a more permanent solution would be using Sockets directly for that request, read the raw request as a string, extract the cookie and set it to the next requests. Here's the code for using Sockets in 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;
}

这篇关于如何在Windows Phone 8中获取HttpOnly Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆