Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere do I set cookies when my web application receives an HttpRequest?
    text
    copied!<p>Where in my C# code do I need to put the code for setting cookies? Firefox shows three cookies when I'm logged in to the system.</p> <p><strong>Login page</strong> </p> <p><code>system.local/interface/Login.php</code></p> <pre><code>POST /interface/Login.php HTTP/1.1 Host: system.local User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3 Accept-Encoding: gzip, deflate Connection: keep-alive Referer: http://system.local/interface/Login.php Cookie: language=en_US.UTF-8; StationID=06ae3ed4d72a33dd951572df84a13725 Content-Type: application/x-www-form-urlencoded Content-Length: 71 user_name=user&amp;password=password123456&amp;language=en&amp;action%3Asubmit=Submit </code></pre> <p><strong>GET response from index.php when logged in</strong> </p> <p><code>http://system.local/interface/index.php</code></p> <pre><code>GET /interface/index.php HTTP/1.1 Host: system.local User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3 Accept-Encoding: gzip, deflate Connection: keep-alive Referer: system.local/interface/Login.php Cookie: language=en_US.UTF-8; StationID=06ae3ed4d72a33dd951572df84a13725; SessionID=3783a8fea972fb99779f7dd3078ed53c </code></pre> <p><strong>Where to put the cookie(s) in this code?</strong> </p> <pre><code>string url; string cookieHeader; string formparam; HttpWebRequest request; HttpWebResponse response; byte[] bytes; url = "http://system.local/interface/Login.php"; formparam = string.Format("user_name=" + textBox1.Text + "&amp;password=" + textBox2.Text + "&amp;language=en&amp;action%3Asubmit=Submit"); request = (HttpWebRequest)HttpWebRequest.Create(url); var cookies = new CookieContainer(); request.ContentType = "Content-Type: application/x-www-form-urlencoded"; request.Method = "POST"; bytes = Encoding.ASCII.GetBytes(formparam); request.ContentLength = bytes.Length; request.KeepAlive = true; using (Stream os = request.GetRequestStream()) { os.Write(bytes, 0, bytes.Length); } response = (HttpWebResponse)request.GetResponse(); cookieHeader = response.Headers["Set-Cookie"]; request = (HttpWebRequest)WebRequest.Create("http://system.local/interface/Login.php"); request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(response.Cookies); request.Headers.Add("Cookie", cookieHeader); response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { //richTextBox1.Text = reader.ReadToEnd().Trim(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload