Note that there are some explanatory texts on larger screens.

plurals
  1. POSession cookies - CookieContainer on stack rather than heap causing issue
    primarykey
    data
    text
    <p>I've had a look here <a href="https://stackoverflow.com/questions/4158448/c-sharp-webrequest-using-cookies">C# WebRequest using Cookies</a> <a href="https://stackoverflow.com/questions/787857/multiple-webrequest-in-same-session">Multiple WebRequest in same session</a> <a href="https://stackoverflow.com/questions/9025075/reuse-connection-with-httpwebrequest-in-c-sharp">Reuse Connection with HttpWebRequest in C#</a> <a href="https://stackoverflow.com/questions/1453560/c-sharp-keep-session-id-over-httpwebrequest">C# keep session id over httpwebrequest</a></p> <p>And that's what I'm doing except I wish to store my CookieContainer as a member (named session_cookie) in my class called connector. My problem is that if I use a temporary object in my code then the cookies work fine:</p> <pre><code>CookieContainer t = new CookieContainer(); HTTPReq = (HttpWebRequest)WebRequest.Create(scriptURL); HTTPReq.CookieContainer = t; </code></pre> <p>But if I use </p> <pre><code>HTTPReq = (HttpWebRequest)WebRequest.Create(scriptURL); HTTPReq.CookieContainer = session_cookie; </code></pre> <p>Then it doesn't work! I cannot figure out why</p> <p>Here is the connector class code:</p> <pre><code>public class Connector { public CookieContainer session_cookie; private string session_id; private HttpWebRequest HTTPReq; private HttpWebResponse Response; //Session oriented connection public string serverRequest(string scriptURL, string payLoad) { try { HTTPReq = (HttpWebRequest)WebRequest.Create(scriptURL); HTTPReq.CookieContainer = session_cookie; HTTPReq.Method = "POST"; //Data arguments byte[] byteArray = Encoding.UTF8.GetBytes(payLoad); HTTPReq.ContentType = "application/x-www-form-urlencoded"; HTTPReq.ContentLength = byteArray.Length; //Get the stream to write into Stream dataStream = HTTPReq.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); Response = (HttpWebResponse)HTTPReq.GetResponse(); Encoding enc = Encoding.GetEncoding(1252); // Western latin alphabet (windows default) //Get the repsonse from the server StreamReader ResponseStream = new StreamReader(Response.GetResponseStream(), enc); string response = ResponseStream.ReadToEnd().Trim(); Response.Close(); ResponseStream.Close(); return response; } catch (WebException ex) { Console.WriteLine(ex.ToString()); return null; } } } </code></pre> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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