简体   繁体   English

需要在HttpWebResponse中访问HttpOnly cookie

[英]Need to access HttpOnly cookie in HttpWebResponse

I am trying to get automatically login into a website using POST method and everything seem to work fine except that my HttPWebResponse method conveniently skips a cookie that is marked as HttpOnly. 我试图使用POST方法自动登录到网站,并且一切正常,但我的HttPWebResponse方法方便地跳过了标记为HttpOnly的cookie。 Is there any way I can read it. 有什么我可以阅读的方法吗?

 public CookieContainer _cookies = new CookieContainer();

down in the code I have 在我的代码中

request.CookieContainer = _cookies;

I have read that when using CookieContainer I should not worry about reading the HttpOnly cookies as they are handled atomically. 我已经读过,使用CookieContainer时,我不必担心读取HttpOnly cookie,因为它们是原子处理的。 But apparently this is not the case. 但是显然不是这样。 Using fiddler I do see that I get the 4 cookies but response.Cookies size if 3 and using the same code gets the next request rejected. 使用小提琴手,我确实看到我得到了4个cookie,但是response.Cookies的大小(如果3)并使用相同的代码将拒绝下一个请求。 Please help!! 请帮忙!!

Full code is as follows: 完整代码如下:

HttpWebRequest request = CreateRequest(uri);
request.Method = "POST";
request.GetRequestStream().Write(data, 0, data.Length);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
return  DecodeResponse(response);

DecodeResponse works as follows DecodeResponse的工作原理如下

foreach (System.Net.Cookie cookie in response.Cookies)
{
     Console.WriteLine("Cookie:");
     Console.WriteLine(cookie.HttpOnly);

     _cookies.Add(new Uri(response.ResponseUri.GetLeftPart(UriPartial.Authority)), cookie);

} }

Cookie HttpOnly Determines whether a page script or other active content can access this cookie. Cookie HttpOnly确定页面脚本或其他活动内容是否可以访问此Cookie。

The code below returns true if the cookie has the HttpOnly attribute and cannot be accessed through a client-side script; 如果cookie具有HttpOnly属性并且无法通过客户端脚本访问,则下面的代码返回true;否则,返回0。 otherwise, false. 否则为假。

    var _cookies = new CookieContainer();
    var request = (HttpWebRequest)WebRequest.Create("http://yourURL.com");
    request.CookieContainer = _cookies;
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    foreach (Cookie cook in response.Cookies)
    {
        Console.WriteLine("Cookie:");
        Console.WriteLine(cook.HttpOnly);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM