我用httpWatch抓到一个网站的cookies是;
C_S_GAMEID=10%3A; NOTETIME=1322235777; SID=24887%3A4f0d728cbe6afed21a5c75024cef5f7d; NOTETIME=1322235777;
这是我的httpWebRequest测试代码(这个网站使用utf-8),       string Url = "http://www.domain.com/?action=valet.test";
       string postDate = "postid=10&hostid=57&isah=1&quantity=1000&notice=test&Submit33=提交";
       PostDate(Url, postDate);        public static string PostDate(string url, string postData)
        {
            byte[] data = Encoding.ASCII.GetBytes(postData);
            CookieContainer cookie = new CookieContainer();            cookie.Add(new Cookie("C_S_GAMEID", "10%3A","", "www.domain.com"));
            cookie.Add(new Cookie("NOTETIME", "1322235777", "", "www.domain.com"));
            cookie.Add(new Cookie("SID", @"24887%3A4f0d728cbe6afed21a5c75024cef5f7d", "", "www.domain.com"));
            cookie.Add(new Cookie("NOTETIME", "1322235777", "", "www.domain.com"));            HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            webRequest.Method = "post";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = data.Length;
            webRequest.CookieContainer = cookie;            //发送数据;
            Stream netStream = webRequest.GetRequestStream();
            netStream.Write(data, 0, data.Length);
            netStream.Close();            //查看返回结果;
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            response.Cookies = webRequest.CookieContainer.GetCookies(webRequest.RequestUri);
            Stream stream = response.GetResponseStream();
            StreamReader sr = new StreamReader(stream, Encoding.Default, true);
            string str = sr.ReadToEnd();
            sr.Close();
            response.Close();            ---------后面省略若干行;不成功啊,还是提示要登录
没多少分了,送不了多少分,真的对不起;

解决方案 »

  1.   

    前面不需要cookie.Add,直接webRequest.CookieContainer = cookie;
    然后再提交其他请求时同样使用webRequest.CookieContainer = cookie;
      

  2.   

    谢谢sp1234兄弟,我多抓几次包,
    如果是这种情况,那就先提交一次取到cookie后再次提交
      

  3.   

    bunliney兄弟,我不明白,我现在有的是一个字符串,难道webRequest.CookieContainer = 字符串值么?
      

  4.   

    http://www.netxk.cn/?p=78
    你看看这个例子
      

  5.   

    CookieContainer cookie = new CookieContainer();
    webRequest.CookieContainer = cookie;
    这样做登录后,就保留了cookie,在做其他要求登录的操作时,可以用
    webRequest.CookieContainer = cookie
    把cookie放进去