in Education by
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)

1 Answer

0 votes
by
I think that your mistake is in the following line: cookie = req.CookieContainer = new CookieContainer(); you are instantiating a new cookie container every time after you write the request stream. Since cookie is a global variable initialize it only once say for example where it's declared and set it before you write the stream: req.CookieContainer = cookie; rs.Write(postbuf, 0, postbuf.Length);

Related questions

0 votes
    Greetings all, I'm trying to localize a .NET/C# project. I'm using string resource files and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 12, 2022 in Education by JackTerrance
0 votes
    I want create expression with reflection in c#. I am target script is: using (var service = new ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    What is the .vs folder used for exactly? It gets created at the base folder of my solution. I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Here is the example: if(value != ageValue) { ageValue = value; } I mean, if we assign the value ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I've created a seperate assembly with a class that is intended to be published through wmi. Then I' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    Is there a built in dll that will give me a list of links from a string. I want to send in ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    C#6 Update In C#6 ?. is now a language feature: // C#1-5 propertyValue1 = myObject != null ? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    C#6 Update In C#6 ?. is now a language feature: // C#1-5 propertyValue1 = myObject != null ? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    C#6 Update In C#6 ?. is now a language feature: // C#1-5 propertyValue1 = myObject != null ? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    I would like to know how to unload an assembly that is loaded into the main AppDomain. I have the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I would like to know how to unload an assembly that is loaded into the main AppDomain. I have the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    C#6 Update In C#6 ?. is now a language feature: // C#1-5 propertyValue1 = myObject != null ? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    Having a problem getting a TreeView control to display node images. The code below works sometimes but fails ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm writing an application to start and monitor other applications in C#. I'm using the System.Diagnostics. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I am investigating the design of a work queue processor where the QueueProcessor retrieves a Command Pattern object ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
...