Need to access HttpOnly cookie in HttpWebResponse

Deadly 提交于 2019-12-10 21:04:55

问题


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. 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. 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. 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

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

}


回答1:


Cookie HttpOnly Determines whether a page script or other active content can access this cookie.

The code below returns true if the cookie has the HttpOnly attribute and cannot be accessed through a client-side script; 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);
    }


来源:https://stackoverflow.com/questions/42607754/need-to-access-httponly-cookie-in-httpwebresponse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!