我用                foreach (Cookie cook in webResponse.Cookies)
                {
                    Console.WriteLine("Cookie:");
                    Console.WriteLine("{0} = {1}", cook.Name, cook.Value);
                    Console.WriteLine("Domain: {0}", cook.Domain);
                    Console.WriteLine("Path: {0}", cook.Path);
                    Console.WriteLine("Port: {0}", cook.Port);
                    Console.WriteLine("Secure: {0}", cook.Secure);                    Console.WriteLine("When issued: {0}", cook.TimeStamp);
                    Console.WriteLine("Expires: {0} (expired? {1})",
                        cook.Expires, cook.Expired);
                    Console.WriteLine("Don't save: {0}", cook.Discard);
                    Console.WriteLine("Comment: {0}", cook.Comment);
                    Console.WriteLine("Uri for comments: {0}", cook.CommentUri);
                    Console.WriteLine("Version: RFC {0}", cook.Version == 1 ? "2109" : "2965");                    // Show the string representation of the cookie.
                    Console.WriteLine("String: {0}", cook.ToString());
                }
读取cookie信息,却什么也没有,显示webResponse.Cookies为0.我用                for (int aa = 0; aa < webResponse.Headers.Count ; aa++)
                {
                    Console.WriteLine(webResponse.Headers.GetKey(aa) + " : " + webResponse.Headers.Get(aa));
                }读取头文件结果为Pragma : no-cache
Transfer-Encoding : chunked
Cache-Control : no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Content-Type : text/html
Date : Sun, 25 May 2008 06:10:58 GMT
Expires : Thu, 19 Nov 1981 08:52:00 GMT
Set-Cookie : PHPSESSID=ca503bf9880b4dd72db87ea7694c9e69; path=/
Server : Apache/2.2.4 (Win32) PHP/5.2.1
X-Powered-By : PHP/5.2.1那么请问这里的Set-Cookie是什么?如何知道这个ID什么时候有效?什么时候过期呢? 谢谢!

解决方案 »

  1.   

    读取cookie用 request.Cookie, 而不是用reponse.Cookie, response cookie是用来写cookie的!
      

  2.   

    webResponse.Headers 也是输出的头信息, 而不是请求的头信息请求头信息用 Request.Headers
      

  3.   

    对啊,我是客户端,模拟一个IE. 我要读服务器给我返回的信息.我是抄的MSDN. 我再去试试.
      

  4.   

    我前面的代码为            HttpWebRequest httpWebRequest;
                HttpWebResponse webResponse;
                Stream stream;
                byte[] byteRequest = System.Text.Encoding.Default.GetBytes(s); //s为我要向服务器提交的数据            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.xxx.com/yyy/zzz/");  //加载URL
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.ContentLength = byteRequest.Length;            stream = httpWebRequest.GetRequestStream();
                stream.Write(byteRequest, 0, byteRequest.Length); //POST信息
                 stream.Close();            webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    我向服务器提交了一个数据,模拟点击登陆按钮.
    然后读取服务器转向的登陆页面及其他消息.我要分析服务器给我的cookie及所有信息,但是为什么没有cookie呢?
      

  5.   

    Cookie是在客户端保存的, 你本地需要有一个cookie Container.
    Set-Cookie : PHPSESSID=ca503bf9880b4dd72db87ea7694c9e69; path=/服务器返回 Set-Cookie就是让你在cookieContainer里面增加这样一个cookie.
    记住, cookie不是在服务器端保存的
      

  6.   

    我也碰到过LZ的问题
    一般情况下,服务器回写的COOKIE是应该在httpwebresponse里的Cookies里得到
    但是有情况只在httpwebresponse.header里,而httpwebresponse.Cookies没有,cookieContainer也没有
    我猜是不是Cache-Control : no-store的原因不知道有谁能清楚点