我正在尝试向页面发出请求,并保存页面响应的cookie。如果我手动浏览http://mydomain.com/mypage.aspx?GUID=4579并在Fiddler中调试响应,我可以看到该页面以cookie响应。那么,为什么我的代码不能保存那个cookie呢?这是我的代码:
const string baseUri = "http://mydomain.com/mypage.aspx?GUID=4579";
CookieContainer cookie = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseUri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
request.CookieContainer = cookie;
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
StreamReader reader = new StreamReader(response.GetResponseStream());
string cookiePage = reader.ReadToEnd();
reader.Close();
Console.WriteLine(cookie.Count);
答案 0 :(得分:2)
您需要在收到回复之前设置CookieContainer
一旦你这样做,一切都会自动发生