473,386 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

WebRequest : How to keep session open ?

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
3 6321
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ron | last post by:
Hi there, I'm trying to 'browse' a website using the HttpWebRequest class by calling the following function (webRequest is declared outside the function): private string GetHtml(string sUrl) {...
4
by: Paul | last post by:
Hello, I tried to execute a button on an aspx webpage using WebRequest, but it failed. When posting data as if I did a login (see code), I just receive the login page, instead of the page I...
9
by: Hasani \(remove nospam from address\) | last post by:
I have a website with 2 aspx pages Foo.aspx, and bar.aspx The content of both files is //in 1 file Hello <%=Session.ToString()%> ================
0
by: WIWA | last post by:
Hi, I want to login to a password protected website and fetch the content of the page behind. I have based my code on http://weblogs.asp.net/jdennany/archive/2005/04/23/403971.aspx. When I use...
3
by: Mario | last post by:
Greetings! Here is the problem i'm facing. I'm supposed to read some user input on Page1, then, depends on what the user inputs, redirect him to Page2 or Page3 and send the entered data with it....
1
by: Mr Flibble | last post by:
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...
6
by: matt | last post by:
hello, i am having trouble doing something. when a user triggers a certain event in my app, i need to initiate another web request to one of my other webpages, programmatically. currently, i do...
6
by: Jack | last post by:
I have a WebRequest object that I use to log into a site and then post some XML. In doing this I set the KeepAlive = true so that it maintains the connection and does operates undo the initial...
3
by: Dave | last post by:
string m_request = some_web_page; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m_request ); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Which works...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.