J
James Johnson
Dear C#Dex,
I am trying to automate a POST to a web page that clicks a button. I
have been able to hit a target web page and run the web page. However,
the button on the page does not click. I can set the target web page to
change to a new URL when I hit it, and that works, but I cannot get the
button to click based on my POST command. Is there some secret to
clicking the button? I have a button named btnGo and I set the value of
btnGo=clicked; however, the button is not clicked. I can, for example,
provide querystring values and the target web page detects these and
will perform logic (such as navigating to another page) based upon the
provided query string, but I can’t get the button to click using a POST
command. The relevant code is shown below:
url= "http://localhost/FirstProject/WebForm2.aspx";
payload = "txtUserID=text&txtPassword=text&btnGo=clicked&";
WebRequest req = WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = {'?', '=', '&'};
byte[] SomeBytes = null;
if (payload != null)
{
int i=0, j;
while(i<payload.Length)
{
j=payload.IndexOfAny(reserved, i);
if (j==-1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i,
payload.Length-i))); //URL encodes and appends to the contents of the
stringbuilder any text remaining in the payload after the last special
char
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i,
j-i))); //appends URL encoded text that is between reserved characters
to the stringbuilder contents
UrlEncoded.Append(payload.Substring(j,1)); //appends literal
reserved char to contents of stringbuilder
i = j+1; //increments index to get text starting point at
position past the position of the current reserved character
}
string str = UrlEncoded.ToString();
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString()); //Encodes
URL string into hexadecimal bytes
req.ContentLength = SomeBytes.Length; //Gets length of string
encoded into bytes that is to be sent as a POST request
//The URL plus the encoded variables
Stream newStream = req.GetRequestStream(); //Create a Stream variable
so we can write it to the Http stream and initialize it with the encoded
stream
newStream.Write(SomeBytes, 0, SomeBytes.Length); //Writes Http stream
to IP address returned by the URI
newStream.Close(); //Free up resources by closing connection
}
else
{
req.ContentLength = 0;
}
result = req.GetResponse(); //WebResponse get response of what
happened as a result of the POST that you just wrote to the URI—-The
button is not clicked--the page stays the same instead of navigating to
the new page!
Stream ReceiveStream = result.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader( ReceiveStream, encode );
Console.WriteLine("\r\nResponse stream received");
Char[] read = new Char[256];
int count = sr.Read( read, 0, 256 );
Console.WriteLine("HTML...\r\n");
while (count > 0)
{
String str = new String(read, 0, count);
Console.Write(str);
count = sr.Read(read, 0, 256);
}
Console.WriteLine("");
Do you have any suggestions as to why the button won't click? Thanks
for your help.
James J.
I am trying to automate a POST to a web page that clicks a button. I
have been able to hit a target web page and run the web page. However,
the button on the page does not click. I can set the target web page to
change to a new URL when I hit it, and that works, but I cannot get the
button to click based on my POST command. Is there some secret to
clicking the button? I have a button named btnGo and I set the value of
btnGo=clicked; however, the button is not clicked. I can, for example,
provide querystring values and the target web page detects these and
will perform logic (such as navigating to another page) based upon the
provided query string, but I can’t get the button to click using a POST
command. The relevant code is shown below:
url= "http://localhost/FirstProject/WebForm2.aspx";
payload = "txtUserID=text&txtPassword=text&btnGo=clicked&";
WebRequest req = WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = {'?', '=', '&'};
byte[] SomeBytes = null;
if (payload != null)
{
int i=0, j;
while(i<payload.Length)
{
j=payload.IndexOfAny(reserved, i);
if (j==-1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i,
payload.Length-i))); //URL encodes and appends to the contents of the
stringbuilder any text remaining in the payload after the last special
char
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i,
j-i))); //appends URL encoded text that is between reserved characters
to the stringbuilder contents
UrlEncoded.Append(payload.Substring(j,1)); //appends literal
reserved char to contents of stringbuilder
i = j+1; //increments index to get text starting point at
position past the position of the current reserved character
}
string str = UrlEncoded.ToString();
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString()); //Encodes
URL string into hexadecimal bytes
req.ContentLength = SomeBytes.Length; //Gets length of string
encoded into bytes that is to be sent as a POST request
//The URL plus the encoded variables
Stream newStream = req.GetRequestStream(); //Create a Stream variable
so we can write it to the Http stream and initialize it with the encoded
stream
newStream.Write(SomeBytes, 0, SomeBytes.Length); //Writes Http stream
to IP address returned by the URI
newStream.Close(); //Free up resources by closing connection
}
else
{
req.ContentLength = 0;
}
result = req.GetResponse(); //WebResponse get response of what
happened as a result of the POST that you just wrote to the URI—-The
button is not clicked--the page stays the same instead of navigating to
the new page!
Stream ReceiveStream = result.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader( ReceiveStream, encode );
Console.WriteLine("\r\nResponse stream received");
Char[] read = new Char[256];
int count = sr.Read( read, 0, 256 );
Console.WriteLine("HTML...\r\n");
while (count > 0)
{
String str = new String(read, 0, count);
Console.Write(str);
count = sr.Read(read, 0, 256);
}
Console.WriteLine("");
Do you have any suggestions as to why the button won't click? Thanks
for your help.
James J.