首页 新闻 会员 周边 捐助

C#用HttpWebRequest中post方法发送时,如何获取cookie

1
悬赏园豆:10 [已解决问题] 解决于 2012-06-12 00:41

请求头里我没有加Expect:100-continue,应该是自动加上的,请问这个是什么啊?

我在向网站提交验证码时,总是返回错误,明明是对也返回错误,哪位大侠指点一下啊。

另外请求头里很多项在webRequest.下都没有,所以我自己使用以下方法添加了,

webRequest.Headers.Add("Cookie",cookie),我刚发现这样加是不正确的,不知道怎样加上去,。

 

StartGet();  //取SID
            long TCheckCode = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000;
            //CheckCode(TCheckCode, textBox1.Text);

            Encoding mye = Encoding.GetEncoding("utf-8");
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://reg.jiayuan.com/libs/xajax/reguser.server.php?processTraceUser");

            string s = "xajax=processTraceUser&xajaxargs%5B%5D=%3Cxjxquery%3E%3Cq%3Evalidate_code%3D" + textBox1.Text + "%3C%2Fq%3E%3C%2Fxjxquery%3E&xajaxargs%5B%5D=validate_code&xajaxr=" + TCheckCode;
            byte[] arrB = mye.GetBytes(s);

            myReq.Method = "post";
            myReq.Accept = "*/*";
            myReq.ContentLength = s.Length;
            myReq.Headers.Set("x-requested-with", "XMLHttpRequest");
            myReq.Headers.Set("Accept-Encoding", "gzip, deflate");
            myReq.Headers.Set("Accept-Language", "zh-cn");
            myReq.Headers.Set("Cache-Control", "no-cache");
            myReq.Headers.Set("sid", SID);
           myReq.Headers.Set("Cookie", "cookie");
            myReq.ContentType = "application/x-www-form-urlencoded";
            myReq.Referer = "http://reg.jiayuan.com/signup/fillbasic.php?bd=204";
            myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.1; .NET4.0C; .NET4.0E; 360SE)";
            // myReq.KeepAlive = true;


            //if (cc.Count == 0)
            //{
            //    myReq.CookieContainer = new CookieContainer();//新建一个杯子
            //    cc = myReq.CookieContainer;//指定拿起这个杯子
            //}
            //else
            //{
            //    myReq.CookieContainer = cc;
            //}


            Stream outStream = myReq.GetRequestStream();
            outStream.Write(arrB, 0, arrB.Length);
            outStream.Close();
            HttpWebResponse myResp = null;
            try
            {
                //接收HTTP做出的响应  
                myResp = (HttpWebResponse)myReq.GetResponse();

            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            Stream ReceiveStream = myResp.GetResponseStream();
            StreamReader readStream = new StreamReader(ReceiveStream, mye);
            Char[] read = new Char[256];
            int count = readStream.Read(read, 0, 256);
            string str = null;
            while (count > 0)
            {
                str += new String(read, 0, count);
                count = readStream.Read(read, 0, 256);
            }


            //myReq.CookieContainer = cc;
            //foreach (Cookie cookie in myResp.Cookies)//把服务器给我们的很多滴水,收集放到杯子里面
            //{
            //    cc.Add(cookie);

            //}


            readStream.Close();
            myResp.Close();
            if (str.Contains("验证码填写错误"))
            {
                richTextBox1.Text = str;
            }
            else if (str.Contains("CDATA[show exactness"))
            {
                richTextBox1.Text = "验证码输入正确";
            }
        }
C#
老虎下山的主页 老虎下山 | 初学一级 | 园豆:43
提问于:2012-06-06 13:23
< >
分享
最佳答案
1
收获园豆:10
悟行 | 专家六级 |园豆:12559 | 2012-06-06 14:11
其他回答(1)
0

COOKIE是不能这样添加的。你可以直接在RESPONSE.COOKIES里操作。

刚细看了你的代码,你是在自己写的应用里模拟网页请求,这个。。。可以使用myReq.CookieContainer.Add

无之无 | 园豆:5095 (大侠五级) | 2012-06-06 13:34

可以提供具体操作代码 吗?谢谢,新人第一次练习,还请见谅,

支持(0) 反对(0) 老虎下山 | 园豆:43 (初学一级) | 2012-06-06 13:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册