none
从一个页面获取cookie,用于post数据到另一个页面,cookie怎么处理? RRS feed

  • 问题

  • 这个例子很特殊(一般都情况下访问页面访问会产生cookie,带着cookie post就完了):这个需要先通过访问MainUrl在客户端留下cookie,然后在VoteUrl投票需要带上这个cookie才能完成投票,若客户端直接访问VoteUrl,改页面不产生cookie,就无法完成投票。需要实现的是,通过按钮每次获取新的cookie进行投票。

    我不知道那些地方该该注释掉,哪些地方该改?再次感谢版主!

    using System;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.IO;
    
    namespace AutoVote
    {
        public partial class Form1 : Form
        {
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Vote(string MainUrl, string VoteUrl,string PostData)
            {
                UTF8Encoding encoding = new UTF8Encoding();
                byte[] data = encoding.GetBytes(PostData);
                //定义Cookie容器
                //CookieContainer cc = new CookieContainer();
                //创建Http请求获取cookie
                HttpWebRequest GetcookieRequest = (HttpWebRequest)WebRequest.Create(MainUrl);
                GetcookieRequest.CookieContainer = cc;
                HttpWebResponse response = (HttpWebResponse)GetcookieRequest.GetResponse();
                cc.Add(response.Cookies);
                //创建Http请求。。。
                HttpWebRequest PostRequest = (HttpWebRequest)WebRequest.Create(VoteUrl);
                PostRequest.Method = "POST";
                PostRequest.ContentType = "application/x-www-form-urlencoded";
                PostRequest.ContentLength = PostData.Length;
                //PostRequest.CookieContainer = cc;
                PostRequest.Referer = "http://www.baidu.com/referer.php";
                //获取Post数据流
                Stream newStream = PostRequest.GetRequestStream();
                StreamWriter myStreamWriter = new StreamWriter(newStream, Encoding.Default);
                //把数据写入HttpWebRequest的Request流  
                newStream.Write(data,0,data.Length);
                //关闭打开对象     
                myStreamWriter.Close();
                newStream.Close();
                //新建一个HttpWebResponse 
                HttpWebResponse PostResponse = (HttpWebResponse)PostRequest.GetResponse();
    }
            public CookieContainer cc;
    
            private void button1_Click(object sender, EventArgs e)//投票 按钮
            {
                Vote("http:///www.baidu.com/Main.php", "http://www.baidu.com/Vote.php", "id=1309");
            }
               
        }
    }

    • 已编辑 ADAN_msdn 2013年11月3日 21:13 完善
    2013年11月1日 19:16

答案

  • 您必须处理BackGroundworker的RunWorkerComplete事件,然后:

     public partial class Form1 : Form
        {
            private CookieContainer cc = null;
    
            privatevoid GetSession(string url)//取cookie
            {
                cc = new CookieContainer();
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.CookieContainer = cc;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                cc.Add(response.Cookies);
            }
            public Form1()
            {
                InitializeComponent();
            }
     
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                string postData = "pic_id=xxxx";
                UTF8Encoding encoding = new UTF8Encoding();
                byte[] data = encoding.GetBytes(postData);
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("url post header");
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = data.Length;
                myRequest.Referer = "url referer";
                myRequest.CookieContainer = cc;
                Stream newStream = myRequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                GetSession("url");
            }
           
            private void backgroundWorker1_RunWorkerCompleted(
        object sender, RunWorkerCompletedEventArgs e)
    {
              System.Net.Cookie cookie = cc.GetCookies(new Uri("url"))["guid"];
                this.textBox1.Text = cookie.Value;
    }
            
            private void button2_Click(object sender, EventArgs e)//投票 按钮
            {
                backgroundWorker1.RunWorkerAsync();
            }
        }

    For Account Validation, please follow "Verify Account+Number" at http://social.msdn.microsoft.com/Forums/en-us/home?forum=reportabug

    For ASP.NET Question, please ask at http://forums.asp.net

    For other questions, you can ask at http://stackexchange.com/sites

    Click and Donate at http://www.freerice.com

    2013年11月3日 1:55
    版主
  • 你好:

    我上面的方法其实你只要一个按钮就可以完成投票啥的了,你耐心等到TextBox出来结果之后继续点击,应该可以重复此过程的。不然获取Cookie可能造成顿卡。


    For Account Validation, please follow "Verify Account+Number" at http://social.msdn.microsoft.com/Forums/en-us/home?forum=reportabug

    For ASP.NET Question, please ask at http://forums.asp.net

    For other questions, you can find a specific forum and then ask at http://stackexchange.com/sites

    Click and Donate at http://www.freerice.com

    2013年11月4日 1:47
    版主

全部回复

  • 验证账户请直接在此处跟帖:

    http://social.msdn.microsoft.com/Forums/en-US/2040f706-fd63-4929-bfb8-4c96b4666a75/verify-your-account-7?forum=reportabug

    至于您的问题,您的GetCookie和BackGroundworker是什么关系呢?另外你的GetCookie貌似直接在Button3的Click事件中,可能导致UI卡顿。建议你写入一个线程或者利用BackGroundworker来完成。


    For Account Validation, please follow "Verify Account+Number" at http://social.msdn.microsoft.com/Forums/en-us/home?forum=reportabug

    For ASP.NET Question, please ask at http://forums.asp.net

    For other questions, you can ask at http://stackexchange.com/sites

    Click and Donate at http://www.freerice.com

    2013年11月2日 3:24
    版主
  • 可否帮我改一下呢,我不会改
    2013年11月2日 17:02
  • (1)获取cookie与post接收数据不是同一个页面,需要先访问页面A,客户端产生cookie后(get方式),才能对页面B post数据,若直接进入页面B,因该页面不产生cookie,无法post。

    (2)如何实现不断地通过页面A获取cookie-带该cookie post数据到页面B-清除cookie,对于这种获取cookie和post数据页面不相同的,不知道该怎么处理!

    2013年11月2日 20:05
  • 您必须处理BackGroundworker的RunWorkerComplete事件,然后:

     public partial class Form1 : Form
        {
            private CookieContainer cc = null;
    
            privatevoid GetSession(string url)//取cookie
            {
                cc = new CookieContainer();
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.CookieContainer = cc;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                cc.Add(response.Cookies);
            }
            public Form1()
            {
                InitializeComponent();
            }
     
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                string postData = "pic_id=xxxx";
                UTF8Encoding encoding = new UTF8Encoding();
                byte[] data = encoding.GetBytes(postData);
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("url post header");
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = data.Length;
                myRequest.Referer = "url referer";
                myRequest.CookieContainer = cc;
                Stream newStream = myRequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                GetSession("url");
            }
           
            private void backgroundWorker1_RunWorkerCompleted(
        object sender, RunWorkerCompletedEventArgs e)
    {
              System.Net.Cookie cookie = cc.GetCookies(new Uri("url"))["guid"];
                this.textBox1.Text = cookie.Value;
    }
            
            private void button2_Click(object sender, EventArgs e)//投票 按钮
            {
                backgroundWorker1.RunWorkerAsync();
            }
        }

    For Account Validation, please follow "Verify Account+Number" at http://social.msdn.microsoft.com/Forums/en-us/home?forum=reportabug

    For ASP.NET Question, please ask at http://forums.asp.net

    For other questions, you can ask at http://stackexchange.com/sites

    Click and Donate at http://www.freerice.com

    2013年11月3日 1:55
    版主
  • 我努力尝试了,但因初学不久,上面的,直接用下面的方法可以吗?我改成下面这样了,再帮我改一下吧谢谢啦,忘了说,这个例子很特殊(一般都情况下访问页面访问会产生cookie,带着cookie post就完了):这个需要先通过访问MainUrl在客户端留下cookie,然后在VoteUrl投票需要带上这个cookie才能完成投票,若客户端直接访问VoteUrl,不产生cookie,就无法完成投票。需要实现的是,通过按钮每次获取新的cookie进行投票。

    我不知道那些地方该该注释掉,哪些地方该改?再次感谢版主!

    using System;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.IO;
    
    namespace AutoVote
    {
        public partial class Form1 : Form
        {
    
            public Form1()
            {
                InitializeComponent();
            }
    
            public void Post(string MainUrl, string VoteUrl,string PostData)
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(MainUrl);
                CookieContainer c = new CookieContainer();
                req.CookieContainer = c;
                req.GetResponse();
                
                UTF8Encoding encoding = new UTF8Encoding();
                byte[] data = encoding.GetBytes(PostData);
                HttpWebRequest PostRequest = (HttpWebRequest)WebRequest.Create(VoteUrl);
                PostRequest.Method = "POST";
                PostRequest.ContentType = "application/x-www-form-urlencoded";
                PostRequest.ContentLength = PostData.Length;
                //PostRequest.CookieContainer = cc;
                PostRequest.Referer = "UrlReferer";
                Stream newStream = PostRequest.GetRequestStream();
                StreamWriter myStreamWriter = new StreamWriter(newStream, Encoding.Default);
                newStream.Write(data,0,data.Length);
                myStreamWriter.Close();
                newStream.Close();
                HttpWebResponse PostResponse = (HttpWebResponse)PostRequest.GetResponse();
                //StreamReader reader = new StreamReader(PostResponse.GetResponseStream(), Encoding.Default);
                //string content = reader.ReadToEnd();
    }
            //public CookieContainer c;
    
            private void button1_Click(object sender, EventArgs e)//投票 按钮
            {
    Post("http://www.baidu.com/main.php", "http://www.baidu.com/vote.php", "id=211");
            }
               
        }
    }




    • 已编辑 ADAN_msdn 2013年11月3日 22:06 完善
    2013年11月3日 20:56
  • 你好:

    我上面的方法其实你只要一个按钮就可以完成投票啥的了,你耐心等到TextBox出来结果之后继续点击,应该可以重复此过程的。不然获取Cookie可能造成顿卡。


    For Account Validation, please follow "Verify Account+Number" at http://social.msdn.microsoft.com/Forums/en-us/home?forum=reportabug

    For ASP.NET Question, please ask at http://forums.asp.net

    For other questions, you can find a specific forum and then ask at http://stackexchange.com/sites

    Click and Donate at http://www.freerice.com

    2013年11月4日 1:47
    版主