下面代码功能说明:获得验证码图片和cookie,但是只能获取图片,不能得到cookie,望高手指点  
            String url = "http://202.114.74.198/GenImg";
            String server = "http://202.114.74.198/";
            HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            httpRequest.Timeout = 2000;
            httpRequest.Accept = "*/*";
            httpRequest.Referer = server;
            httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; Tablet PC 2.0)";
            httpRequest.CookieContainer = new CookieContainer();
            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            Stream pic = httpResponse.GetResponseStream();
            Image img = Image.FromStream(pic);
            this.pictureBox1.Image = img;        //显示验证码图片
            this.textBox1.Text = httpResponse.Cookies.Count.ToString();         //显示cookies的记录数   运行为0
            string cookie = httpResponse.GetResponseHeader("Cookie");

解决方案 »

  1.   

         #region 读取 Cookie
         string strCookie=myWebResponse.Headers["Set-Cookie"];
         AddCookieWithCookieHead(CookieCol,strCookie,uri.Host);
         #endregion
      #region 从包含多个 Cookie 的字符串读取到 CookieCollection 集合中
      private static void AddCookieWithCookieHead(CookieCollection cookieCol,string cookieHead,string defaultDomain)
      {
       if(cookieHead==null)return;
       string[] ary=cookieHead.Split(';');
       for(int i=0;i   for(int i=0;i   {
        Cookie ck=GetCookieFromString(ary[i].Trim(),defaultDomain);
        if(ck!=null)
        {
         cookieCol.Add(ck);
        }
       }
      }
      #endregion
      #region 读取某一个 Cookie 字符串到 Cookie 变量中
      private static Cookie GetCookieFromString(string cookieString,string defaultDomain)
      {
       string[] ary=cookieString.Split(',');
       Hashtable hs=new Hashtable();
       for(int i=0;i   {
        string s=ary[i].Trim();
        int index=s.IndexOf("=");
        if(index>0 && index    {
         hs.Add(s.Substring(0,index),s.Substring(index+1));
        }
       }
       Cookie ck=new Cookie();
       foreach(object Key in hs.Keys)
       {
        if(Key.ToString()=="path")ck.Path=hs[Key].ToString();    else if(Key.ToString()=="expires")
        {
         //ck.Expires=DateTime.Parse(hs[Key].ToString();
        } 
        else if(Key.ToString()=="domain")ck.Domain=hs[Key].ToString();
        else
        {
         ck.Name=Key.ToString();
         ck.Value=hs[Key].ToString();
        }
       }
       if(ck.Name=="")return null;
       if(ck.Domain=="")ck.Domain=defaultDomain;
       return ck;
      }
      #endregion
      

  2.   

    Response 是写cookie的
    Request  是读cookie的
      

  3.   

    在ASP帮助文档中的说明更直观,比NET说明更优秀,他是那么描述的:
    Request 对象
    Request 对象在 HTTP 请求期间,检索客户端浏览器传递给服务器的值Response 对象
    使用 Response 对象可以将输出发送到客户端
      

  4.   

    string strCookie=myWebResponse.Headers["Set-Cookie"];这样还是得不到cookie
      

  5.   

     this.textBox1.Text = httpRequest.CookieContainer.GetCookieHeader(new Uri(url));