set-cookies不为null但cookiecontainer为空

时间:2014-02-11 20:43:28

标签: c# .net httpwebrequest

登录后,会为后续请求生成Cookie。

我试图通过发送“POST”请求来获取cookie。

在响应标题中,有一个“set-cookies”字段,其值如下:“value1 = value1,value2 = value2 ......”;

但是,我为HttpWebRequest创建的CookieContainer仍为空。

那么发生了什么,我怎样才能填充我的CookieContainer?

任何建议都将不胜感激。

string loginData = "################################";
            CookieContainer cookiecontainer = new CookieContainer();
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://www.ftwilliam.com/cgi-bin/Enter.cgi");
            myRequest.CookieContainer = cookiecontainer;                
            myRequest.Method = "POST";
            myRequest.KeepAlive = true;
            myRequest.AllowAutoRedirect = false;
            myRequest.Referer = "https://www.ftwilliam.com/cgi-bin/Enter.cgi";
            myRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] loginDataBytes = encoding.GetBytes(loginData);
            myRequest.ContentLength = loginDataBytes.Length;
            Stream stream = myRequest.GetRequestStream();
            stream.Write(loginDataBytes, 0, loginDataBytes.Length);
            stream.Close();
            HttpWebResponse res = (HttpWebResponse)myRequest.GetResponse();

0 个答案:

没有答案