读书人

使用HttpWebrequest时操作Cookie的有关

发布时间: 2012-04-10 21:03:56 作者: rapoo

使用HttpWebrequest时操作Cookie的问题
具体需求如下:
假设我要用一个程序模拟下面这个网址中《确定》按钮的功能:http://list.qq.com/cgi-bin/qf_invite?id=3ef0963350a0570a66a32f2d3dbffc75c190b23cc7690702

此程序需要在短时间内多次模拟发送请求的操作。
但是它自从第三次起会要求输入验证码,甚至会直接提示操作频繁稍后再试。

问题1:
能不能每次操作都不用输入验证码直接提交?
若能,怎么实现?

问题2:
如何从它返回的提示输入验证码的页面中提取验证码图片?
因为根据验证码图片地址获取的话每次都会重新生成一个新的图片,将会导致验证失败。

我当前使用的代码如下:

C# code
string postData = "t=qf_booked_feedback&id=3ef0963350a0570a66a32f2d3dbffc75c190b23cc7690702&to=999999999@qq.com";            //验证码            string verifycode_cn = txtVal.Text.Trim();                //如果输入了验证码            postData += verifycode_cn.Length > 0 ? "&verifycode_cn=" + verifycode_cn : "";            ASCIIEncoding encoding=new ASCIIEncoding();             byte[] data = encoding.GetBytes(postData);                             // Prepare web request...               HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://list.qq.com/cgi-bin/qf_compose_send");                           ///测试代码            //CookieContainer mc = new CookieContainer();            //myRequest.CookieContainer = mc;            ///测试结束                        myRequest.Method = "POST";               myRequest.ContentType="application/x-www-form-urlencoded";               myRequest.ContentLength = data.Length;              Stream newStream=myRequest.GetRequestStream();                             // Send the data.               newStream.Write(data,0,data.Length);               newStream.Close();                          // Get response               HttpWebResponse myResponse=(HttpWebResponse)myRequest.GetResponse();            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"));               string content = reader.ReadToEnd();            reader.Close();            //测试用代码,查看结果            this.richTextBox1.AppendText(content);            this.webBrowser1.DocumentText = content; 


[解决办法]
返回的里面应该取不到图片把,貌似只能默认每次都输入验证码。
现读取验证码图片,然后根据该cookie去post

读书人网 >C#

热点推荐