Spunky
10/7/2002 6:40:00 PM
Hi,
I am having problems with maintaing state using the CookieContainer.
Sample code is below. My problem is that I am able to receive a cookie that
a site sets when I post to it. When I want to use that cookie for a
subsequent visit to the site, the site is not able to find the cookie. Can
someone help me on this?
Thanks
Abdullah.
CookieCollection cc;
HttpWebRequest WReq = (HttpWebRequest)WebRequest.Create(action);
WReq.Method = "POST";
WReq.ContentLength = postData.Length;
WReq.ContentType = "application/x-www-form-urlencoded";
WReq.CookieContainer = new CookieContainer();
StreamWriter myWriter = null;
try
{
myWriter = new StreamWriter(WReq.GetRequestStream());
myWriter.Write(postData);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
myWriter.Close();
}
HttpWebResponse WResp = (HttpWebResponse)WReq.GetResponse();
responseUri = WResp.ResponseUri.ToString();
if (WResp.Cookies.Count > 0)
cc = WResp.Cookies;
/*
* Read the response stream
*/
WResp.GetResponseStream().Close();
// Do the second call
HttpWebRequest WReq1 = (HttpWebRequest)WebRequest.Create(action);
WReq1.Method = "POST";
WReq1.ContentLength = postData.Length;
WReq1.ContentType = "application/x-www-form-urlencoded";
WReq1.CookieContainer = new CookieContainer();
if (cc != null && cc.Count > 0)
WReq1.CookieContainer.Add(cc);
StreamWriter myWriter1 = null;
try
{
myWriter1 = new StreamWriter(WReq1.GetRequestStream());
myWriter1.Write(postData);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
myWriter1.Close();
}
HttpWebResponse WResp1 = (HttpWebResponse)WReq1.GetResponse();
/*
* Read the response stream
* Here I find that the site is telling me that it cannot find the cookie.
*/
WResp1.GetResponseStream().Close();