0

I've had a look here C# WebRequest using Cookies Multiple WebRequest in same session Reuse Connection with HttpWebRequest in C# C# keep session id over httpwebrequest

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:

CookieContainer t = new CookieContainer();
HTTPReq = (HttpWebRequest)WebRequest.Create(scriptURL);
HTTPReq.CookieContainer = t;

But if I use

HTTPReq = (HttpWebRequest)WebRequest.Create(scriptURL);
HTTPReq.CookieContainer = session_cookie;

Then it doesn't work! I cannot figure out why

Here is the connector class 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;
        }
    }    
}

Any ideas?

Community
  • 1
  • 1
friartuck
  • 2,954
  • 4
  • 33
  • 67

1 Answers1

0

You are missing a constructor for your Connector class which needs to initialize your CookieContainer property. You should also use a CookieCollection for capturing the cookies from the response and sending to the next request. Something like this should work:

public class Connector
{
    public CookieContainer session_cookie;
    private CookieCollection cookies;
    private string session_id;
    private HttpWebRequest HTTPReq;
    private HttpWebResponse Response;

    public Connector()
    {
        session_cookie = new CookieContainer();
        cookies = new CookieCollection();
    }

    //Session oriented connection
    public string serverRequest(string scriptURL, string payLoad) 
    {
        try
        {
        HTTPReq = (HttpWebRequest)WebRequest.Create(scriptURL);
        HTTPReq.CookieContainer = session_cookie;
            HTTPReq.CookieContainer.Add(cookies);
        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();
            cookies = Response.Cookies; // capture cookies from response for next request

        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;
        }
    }

}
Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85