WebRequest : How to keep session open ?

P

Paul

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=asecret&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=asecret&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
 
C

Christof Nordiek

Paul said:
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=asecret&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
 
P

Paul

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
 
P

Paul

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top