C# 删除Cookie/循环登录&;注销请求
我正在做一个项目,登录到一个网站,而不是立即注销,然后重新开始。我的问题是,我不确定如何正确注销,然后重新发送。关闭应用程序并重新启动,用户将再次登录,以便清除明显的cookieC# 删除Cookie/循环登录&;注销请求,c#,cookies,httpwebrequest,C#,Cookies,Httpwebrequest,我正在做一个项目,登录到一个网站,而不是立即注销,然后重新开始。我的问题是,我不确定如何正确注销,然后重新发送。关闭应用程序并重新启动,用户将再次登录,以便清除明显的cookie private void Form1_Load(object sender, EventArgs e) { WebRequest request; string postData; byte[] byteArray; Stream dataS
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¶m=0¶m=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);
}
}
}
如何实现这样的循环过程?下面的psuedocode应该适合您 请注意,在登录和注销请求上重复使用了相同的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。但如果您需要更仔细地了解到底发生了什么,它仍然应该提供有用的调试信息。这里的示例:“因此,它的明显cookie将被清除”。“不”被清除你的意思是?嗨,我完全忘记了这一点,但非常感谢,我会在一个小时左右尝试它!标记为答案,因为它在我看来是正确的,而且您似乎对此有很好的理解。