M
Mr Flibble
OK I logon to a web site and I manage to get an SMSESSION cookie that I
then store in a variable called _session (a class scoping variable). I
do this by calling a logon URL and setting a cookie to SMCHALLANGE=YES
to allow me to obtain a session. I then iterate the cookie collection
to extract the SMSESSION value. All is good (so far). This is when the
sky turns grey and the rain starts to fall. I then use this SMSESSION
in a web request and I get a response from the web server but I'm unable
to get any cookies. webResponse.Cookies collection from here on in
always has a count of zero. Whether I try using a new cookie container,
re-using the old one, no matter what I do I seem to be unable to
retrieve any subsequent cookies. I'm starting to think it may be better
to iterate the HTTP headers and get the values myself. The SMSESSION
value changes on each request and so not being able to get the updated
value is a major issue.
-- code snippet follows --
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.CookieContainer = new CookieContainer();
webRequest.CookieContainer.SetCookies(webRequest.RequestUri, _session);
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(payload);
webRequest.ContentLength = data.Length;
Stream newStream = webRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse webResponse;
webResponse = (HttpWebResponse)webRequest.GetResponse();
foreach (Cookie c in webResponse.Cookies)
{
if (c.Name == "SMSESSION")
{
_session = c.ToString();
break;
}
}
-- snip --
then store in a variable called _session (a class scoping variable). I
do this by calling a logon URL and setting a cookie to SMCHALLANGE=YES
to allow me to obtain a session. I then iterate the cookie collection
to extract the SMSESSION value. All is good (so far). This is when the
sky turns grey and the rain starts to fall. I then use this SMSESSION
in a web request and I get a response from the web server but I'm unable
to get any cookies. webResponse.Cookies collection from here on in
always has a count of zero. Whether I try using a new cookie container,
re-using the old one, no matter what I do I seem to be unable to
retrieve any subsequent cookies. I'm starting to think it may be better
to iterate the HTTP headers and get the values myself. The SMSESSION
value changes on each request and so not being able to get the updated
value is a major issue.
-- code snippet follows --
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.CookieContainer = new CookieContainer();
webRequest.CookieContainer.SetCookies(webRequest.RequestUri, _session);
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(payload);
webRequest.ContentLength = data.Length;
Stream newStream = webRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse webResponse;
webResponse = (HttpWebResponse)webRequest.GetResponse();
foreach (Cookie c in webResponse.Cookies)
{
if (c.Name == "SMSESSION")
{
_session = c.ToString();
break;
}
}
-- snip --