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
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