By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,649 Members | 1,560 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,649 IT Pros & Developers. It's quick & easy.

WebRequest : How to keep session open ?

P: n/a
Hello,

First I want to refer to the problem "WebRequest : execute a button" of
a few days ago.
The way I solved it, I loose my session, and as a consequence my
session variables. I don't want to keep those variables, as an
alternative, as ViewState variables because I don't want to transfer to
many hidden fields.

This is the code I use :

string uri = @"http://localhost/Login.aspx";
WebRequest req = WebRequest.Create(uri);
WebResponse response = req.GetResponse();
Stream respstream = response.GetResponseStream();
StreamReader reader = new StreamReader(respstream, Encoding.ASCII);
string resphtml = reader.ReadToEnd();
response.Close();
respstream.Close();

string html = resphtml.Replace("\n", " ");
Regex r = new Regex("<input[\\t ]+type=\"hidden\"[\\t
]+name=\"__VIEWSTATE\" value=\"(?<viewstate>[^\"]+)\"");
Match m = r.Match(html);
string viewstate = "";
if (m.Success)
{ viewstate = m.Groups["viewstate"].Value; }

string requeststr =
string.Format("__VIEWSTATE={0}&name=myname&pwd=ase cret&login=Login",
UrlEncoder.Encode(viewstate));

// I only can make it work by making a new request (and making a new
session as a consequence) :
IS THERE A WAY TO REUSE THE SAME REQUEST ? (because this way I suppose
you also keep the session open)

WebRequest req2 = WebRequest.Create(uri);
req2.Method = "POST";
req2.ContentType = "application/x-www-form-urlencoded";
byte[] encodedBytes = Encoding.UTF8.GetBytes(requeststr);
req2.ContentLength = encodedBytes.Length;

Stream requestStream = req2.GetRequestStream();
requestStream.Write(encodedBytes, 0, encodedBytes.Length);
requestStream.Close();

WebResponse response2 = req2.GetResponse();
Stream respstream2 = response2.GetResponseStream();
StreamReader reader2 = new StreamReader(respstream2, Encoding.ASCII);
resphtml = reader2.ReadToEnd();

respstream2.Close();
response2.Close();
This is the code where I try to keep the session open by reusing the
same WebRequest object :
string uri = @"http://localhost/Login.aspx";

WebRequest req = WebRequest.Create(uri);
WebResponse response = req.GetResponse();

Stream respstream = response.GetResponseStream();
StreamReader reader = new StreamReader(respstream, Encoding.ASCII);
string resphtml = reader.ReadToEnd();

string html = resphtml.Replace("\n", " ");
Regex r = new Regex("<input[\\t ]+type=\"hidden\"[\\t
]+name=\"__VIEWSTATE\" value=\"(?<viewstate>[^\"]+)\"");

Match m = r.Match(html);
string viewstate = "";
if (m.Success)
{ viewstate = m.Groups["viewstate"].Value; }

string requeststr =
string.Format("__VIEWSTATE={0}&name=myname&pwd=ase cret&login=Login",
UrlEncoder.Encode(viewstate));

req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] encodedBytes = Encoding.UTF8.GetBytes(requeststr);

// this is the way I try to write a response ('push a button')
// using the same WebRequest object (to keep the session); I can't
// see what I should do else
Stream requestStream = req.GetRequestStream();

// FAILS : ERRORMESSAGE : The connection was closed unexpectedly.
requestStream.Write(encodedBytes, 0, encodedBytes.Length);
response = req.GetResponse();
respstream = response.GetResponseStream();
reader = new StreamReader(respstream, Encoding.ASCII);
resphtml = reader.ReadToEnd();

requestStream.Close();
respstream.Close();
response.Close();

Thanks for all suggestions, hints or solutions,

Paul

Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Paul" <pa*************@hotmail.com> schrieb im Newsbeitrag
news:11**********************@g43g2000cwa.googlegr oups.com...
Hello,

First I want to refer to the problem "WebRequest : execute a button" of
a few days ago.
The way I solved it, I loose my session, and as a consequence my
session variables. I don't want to keep those variables, as an
alternative, as ViewState variables because I don't want to transfer to
many hidden fields.

This is the code I use :

string uri = @"http://localhost/Login.aspx";
WebRequest req = WebRequest.Create(uri);
WebResponse response = req.GetResponse();
Stream respstream = response.GetResponseStream();
StreamReader reader = new StreamReader(respstream, Encoding.ASCII);
string resphtml = reader.ReadToEnd();
response.Close();
respstream.Close();

string html = resphtml.Replace("\n", " ");
Regex r = new Regex("<input[\\t ]+type=\"hidden\"[\\t
]+name=\"__VIEWSTATE\" value=\"(?<viewstate>[^\"]+)\"");
Match m = r.Match(html);
string viewstate = "";
if (m.Success)
{ viewstate = m.Groups["viewstate"].Value; }

string requeststr =
string.Format("__VIEWSTATE={0}&name=myname&pwd=ase cret&login=Login",
UrlEncoder.Encode(viewstate));

// I only can make it work by making a new request (and making a new
session as a consequence) :
IS THERE A WAY TO REUSE THE SAME REQUEST ? (because this way I suppose
you also keep the session open)

WebRequest req2 = WebRequest.Create(uri);
req2.Method = "POST";
req2.ContentType = "application/x-www-form-urlencoded";
byte[] encodedBytes = Encoding.UTF8.GetBytes(requeststr);
req2.ContentLength = encodedBytes.Length;

Stream requestStream = req2.GetRequestStream();
requestStream.Write(encodedBytes, 0, encodedBytes.Length);
requestStream.Close();

WebResponse response2 = req2.GetResponse();
Stream respstream2 = response2.GetResponseStream();
StreamReader reader2 = new StreamReader(respstream2, Encoding.ASCII);
resphtml = reader2.ReadToEnd();

respstream2.Close();
response2.Close();

<snip>
Hi Paul,

how does your server manage Session state.
Could be by Cookie(defalut) or By Url.
I don't see where you resubmit cookies or change the url.
You should search for that.

BTW. did you consider WebService.

Christof
Nov 17 '05 #2

P: n/a
Thanks Christof,

Webservices would imply a rewriting if the webapplication.
If possible this should be avoided.

I will have a look at how microsoft manages session state with asp.net.
I should have thought about that ..

Paul

Nov 17 '05 #3

P: n/a
Hello again,

The solution I tried was just nonsense.
In case someone has the same problem :
WebResponse does not support cookies in the compact framework,so you
need cookieless state support for session management :
edit the Web.Config file : cookieless="true"

Paul

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.