473,473 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

WebRequest : execute a button

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 should get after login.
The button I tried to 'push' (by adding &login=Login in de posted data,
where login is the name of the login button - I verified this data by
capturing the data of a request using a webbrowser) has not executed.

This is how I proceeded :

- First I requested the webpage in the standard way when you use
WebRequest :

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();

- From the response I used regex to obtain the viewstate that I will
use in the second request where I just want to push a button on the
loginpage :

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

- I prepare the string I will post; notice "login=Login" by which I
pushed the button. I also did an url encoding (space to +, strange
chars to %xx) :

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

- Then a do a POST with the data in requeststr you see a little higher
:

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

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

response = req.GetResponse();
respstream = response.GetResponseStream();
reader = new StreamReader(respstream, Encoding.ASCII);
resphtml = reader.ReadToEnd();
respstream.Close();
response.Close();

I also tried it with new objects, instead of reusing the objects of the
first request (same result).

Instead of executing the login button I just receive the login page
again, as if I requested that page without the POSTed data.

Has anyone tried something similar or has anybody suggestions about
what I did wrong ?

Thanks for any help,

Paul

Nov 19 '05 #1
4 1415
Paul,

Do you have access to the code for the site you are trying to access?
It might be easier for you to expose a web service from that website, and
consume it in your code. The amount of code that you are using which is
dependent on things you shouldn't have to deal with (parsing viewstate out
of the page, for instance) is staggering.

Exposing a web service, if possible, would be a much, much better
solution.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul" <pa*************@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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 should get after login.
The button I tried to 'push' (by adding &login=Login in de posted data,
where login is the name of the login button - I verified this data by
capturing the data of a request using a webbrowser) has not executed.

This is how I proceeded :

- First I requested the webpage in the standard way when you use
WebRequest :

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();

- From the response I used regex to obtain the viewstate that I will
use in the second request where I just want to push a button on the
loginpage :

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

- I prepare the string I will post; notice "login=Login" by which I
pushed the button. I also did an url encoding (space to +, strange
chars to %xx) :

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

- Then a do a POST with the data in requeststr you see a little higher
:

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

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

response = req.GetResponse();
respstream = response.GetResponseStream();
reader = new StreamReader(respstream, Encoding.ASCII);
resphtml = reader.ReadToEnd();
respstream.Close();
response.Close();

I also tried it with new objects, instead of reusing the objects of the
first request (same result).

Instead of executing the login button I just receive the login page
again, as if I requested that page without the POSTed data.

Has anyone tried something similar or has anybody suggestions about
what I did wrong ?

Thanks for any help,

Paul

Nov 19 '05 #2
you don't urlencode the postdata, you urlencode each value

string.Format("__VIEWSTATE={0}&name={1}&pwd={2}&lo gin=Login",
UrlEncoder.Encode(viewstate),
UrlEncoder.Encode(name),
UrlEncoder.Encode(pwd))

you don't want the "&" and "=" delimiters encoded. no special chars are
allowed in a value name.

note: the leading "__" in VIEWSTATE is a violation of W3C standards. don't
follow ms's example in your own field names.

-- bruce (sqlwork.com)
"Paul" <pa*************@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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 should get after login.
The button I tried to 'push' (by adding &login=Login in de posted data,
where login is the name of the login button - I verified this data by
capturing the data of a request using a webbrowser) has not executed.

This is how I proceeded :

- First I requested the webpage in the standard way when you use
WebRequest :

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();

- From the response I used regex to obtain the viewstate that I will
use in the second request where I just want to push a button on the
loginpage :

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

- I prepare the string I will post; notice "login=Login" by which I
pushed the button. I also did an url encoding (space to +, strange
chars to %xx) :

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

- Then a do a POST with the data in requeststr you see a little higher
:

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

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

response = req.GetResponse();
respstream = response.GetResponseStream();
reader = new StreamReader(respstream, Encoding.ASCII);
resphtml = reader.ReadToEnd();
respstream.Close();
response.Close();

I also tried it with new objects, instead of reusing the objects of the
first request (same result).

Instead of executing the login button I just receive the login page
again, as if I requested that page without the POSTed data.

Has anyone tried something similar or has anybody suggestions about
what I did wrong ?

Thanks for any help,

Paul

Nov 19 '05 #3
Paul wrote:
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 should get after login.
The button I tried to 'push' (by adding &login=Login in de posted
data, where login is the name of the login button - I verified this
data by capturing the data of a request using a webbrowser) has not
executed.

This is how I proceeded :

- First I requested the webpage in the standard way when you use
WebRequest :

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();

- From the response I used regex to obtain the viewstate that I will
use in the second request where I just want to push a button on the
loginpage :

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

- I prepare the string I will post; notice "login=Login" by which I
pushed the button. I also did an url encoding (space to +, strange
chars to %xx) :

string requeststr =
string.Format("__VIEWSTATE={0}&name=myname&pwd=not ofyourbusiness&login
=Login",viewstate); string requrlencoded =
UrlEncoder.Encode(requeststr);
As Bruce already pointed out, that's not going to work. You have to
encode all values of the query string individually, unless UrlEncoder
is meant to be a non BCL class which handles entire strings (unlike
HttpUtility.UrlEncode()).
- Then a do a POST with the data in requeststr you see a little higher
:

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

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

response = req.GetResponse();
respstream = response.GetResponseStream();
reader = new StreamReader(respstream, Encoding.ASCII);
resphtml = reader.ReadToEnd();
respstream.Close();
response.Close();

I also tried it with new objects, instead of reusing the objects of
the first request (same result).
You confuse objects with variables. For the sencond request, you reuse
your local variables, but have a new HttpWebRequest, a new
HttpWebResponse, and a new StreamReader.
Instead of executing the login button I just receive the login page
again, as if I requested that page without the POSTed data.

Has anyone tried something similar or has anybody suggestions about
what I did wrong ?


You're missing session management. Add a CookieContainer instance to
your HttpWebRequest to allow for cookie management and use it
throughout your session.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 19 '05 #4
Thanks everybody !
The problem was the urlencode indeed.

Paul

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
3
by: Paul | last post by:
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...
1
by: Sujith Jagini | last post by:
------------------------------------------------------------------ <input type="hidden" name="__VIEWSTATE" value="dDwtMTUzMzQ4ODY4ODs7Pleb3NLElxrFpPbkFWoEst9bdqIM" /> <span id="Label1"...
2
by: Stephen Miller | last post by:
I have an ASPX report and I want to capture the rendered HTML and write to a file on the webserver. Several posts suggest using WebRequest to make a second call to the page, and screen-scrape the...
0
by: buran | last post by:
Dear ASP.NET Programmers, Please consider the intranet site with the following forms authentication page http://localhost/database/login.aspx. The usernames and passwords are stored in the SQL...
0
by: Frank S. | last post by:
Hello, when I try to execute a web request from a .NET client application through a proxy, the server returns a 401 (not authorized) error message. It works when i bypass the proxy or when i...
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...
10
by: Steve | last post by:
I am trying to create a downloader which will bypass the saveas box in the WebBrowser control. I have the file downloading, but I don't know what the filename is that is being passed through the...
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...
0
by: CodeMedic42 | last post by:
I have started to play with connecting to a .Net web service that I created. I wanted to figure out how to dynamicaly connect to it without creating a web reference to it. I have so far been able to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.