I'm trying to connect to a website that uses cookies. My login is as follows:
private void button1_Click(object sender, EventArgs e)
{
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseurl);
req.Referer = referer;
req.AllowAutoRedirect = true;
req.KeepAlive = true;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] postbuf = Encoding.ASCII.GetBytes(login);
req.ContentLength = postbuf.Length;
Stream rs = req.GetRequestStream();
rs.Write(postbuf, 0, postbuf.Length);
rs.Close();
cookie = req.CookieContainer = new CookieContainer();
WebResponse resp = req.GetResponse();
string s = "";
foreach (Cookie c in cookie.GetCookies(req.RequestUri))
{
s += ("Cookie['" + c.Name + "']: " + c.Value);
}
textBox1.Text = s;
resp.Close();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
}
The cookie has 2 keys with values. Then I try to send a post request to the website as follows:
private void button2_Click(object sender, EventArgs e)
{
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(search);
req.Referer = referer;
req.AllowAutoRedirect = true;
req.KeepAlive = true;
req.CookieContainer = cookie;
StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream());
if (reader != null)
{
string data = reader.ReadToEnd();
reader.Close();
textBox1.Text = data;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
}
But I get an error that it cannot authenticate. When I login using firefox, I see that the cookie contains much more entries.
What is wrong with my code?
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)