问题
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