0

我正在制作一个登录网站的项目,而不是立即注销并重新开始。那么我的问题是cookies 我很不确定如何正确注销然后重新发送。关闭应用程序并重新启动它会使用户再次登录,因此其明显的 cookie 将被清除。

   private void Form1_Load(object sender, EventArgs e)
    {
        WebRequest request;
        string postData;
        byte[] byteArray;
        Stream dataStream;
        while (true)
        {
            try
            {
                HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("http://www.********/index.php");

                ASCIIEncoding encoding = new ASCIIEncoding();
                postData = "param=example&param=0&param=bigboy";
                byte[] data = encoding.GetBytes(postData);
                httpWReq.Method = "POST";
                httpWReq.ContentType = "application/x-www-form-urlencoded";
                httpWReq.ContentLength = data.Length;
                httpWReq.KeepAlive = false;

                httpWReq.CookieContainer = new CookieContainer();
                using (Stream stream = httpWReq.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                    stream.Close();
                }
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
            }
        }
    }

可以做些什么来实现这样的循环过程?

4

1 回答 1

0

像下面的伪代码这样的东西应该适合你。

请注意在登录和注销请求中重复使用相同的 CookieContainer 对象。

static void Main(string[] args)
{
    while (true)
    {
        try
        {
            CookieContainer cookies = new CookieContainer();


            HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create("http://www.********/index.php");
            loginRequest.CookieContainer = cookies;

            // Configure login request headers and data, write to request stream, etc.

            HttpWebResponse loginResponse = (HttpWebResponse)loginRequest.GetResponse();


            HttpWebRequest logoutRequest = (HttpWebRequest)WebRequest.Create("http://www.********/logout.php");
            logoutRequest.CookieContainer = cookies;

            // Configure logout request headers and data, write to request stream, etc.

            HttpWebResponse logoutResponse = (HttpWebResponse)logoutRequest.GetResponse();
        }
        catch (Exception err)
        {
            Console.WriteLine(err.Message);
        }
    }
}

试试这样的事情,让我知道它是怎么回事。

另外:尝试调试响应对象的 Cookie 属性。根据请求,它是 CookieCollection,而不是CookieContainer。但是,如果您需要仔细查看到底发生了什么,它仍然应该提供有用的调试信息。此处示例:http: //goo.gl/L2MMrj

于 2013-10-17T05:03:43.880 回答