HttpWebRequest to ASPX for Postback

T

TJO

I am working with the HttpWebRequest agains a simple aspx page that I
created. The page has two panels one for the input of two html input boxes
and the secone panel that displays the values of the input boxes on
postback.

I make my first web GET request like this:
objRequest1 = (HttpWebRequest)System.Net.HttpWebRequest.Create(uri);
objRequest1.Method = "GET";
objRequest1.ContentType = "text/html";
objRequest1.CookieContainer = new CookieContainer();
objRequest1.AllowAutoRedirect = true;

objResponse1 = (HttpWebResponse)objRequest1.GetResponse();
sr = new StreamReader(objResponse1.GetResponseStream());
result = sr.ReadToEnd();
sr.Close();

I then receive a session cookie that I put in my 2nd request like this:

string postData = "txt_name=tim&txt_address=261 Trailway"

objRequest2 = (HttpWebRequest)System.Net.HttpWebRequest.Create(uri);
objRequest2.Method = "POST";
objRequest2.AllowAutoRedirect = true;
objRequest2.ContentLength = postData.Length;
objRequest2.ContentType = "application/x-www-form-urlencoded";
objRequest2.Timeout = 100000;
objRequest2.CookieContainer = new CookieContainer();
objRequest2.CookieContainer.Add(objResponse1.Cookies);
myWriter = new StreamWriter(objRequest2.GetRequestStream());
myWriter.Write(postData);

objResponse2 = (HttpWebResponse)objRequest2.GetResponse();
sr = new StreamReader(objResponse2.GetResponseStream());
result = sr.ReadToEnd();
sr.Close();

I am hoping this 2nd request trigger the postback and therefore the output
data in the 2nd panel but it does not. What am I doing wrong?

Thank for you help.
 
J

Joerg Jooss

"TJO" spoke:
I am working with the HttpWebRequest agains a simple aspx page that I
created. The page has two panels one for the input of two html
input boxes and the secone panel that displays the values of the
input boxes on postback.

I make my first web GET request like this:
objRequest1 = (HttpWebRequest)System.Net.HttpWebRequest.Create(uri);
objRequest1.Method = "GET";
objRequest1.ContentType = "text/html";
objRequest1.CookieContainer = new CookieContainer();
objRequest1.AllowAutoRedirect = true;

objResponse1 = (HttpWebResponse)objRequest1.GetResponse();
sr = new StreamReader(objResponse1.GetResponseStream());
result = sr.ReadToEnd();
sr.Close();

I then receive a session cookie that I put in my 2nd request like
this:

string postData = "txt_name=tim&txt_address=261 Trailway"

objRequest2 = (HttpWebRequest)System.Net.HttpWebRequest.Create(uri);
objRequest2.Method = "POST";
objRequest2.AllowAutoRedirect = true;
objRequest2.ContentLength = postData.Length;
objRequest2.ContentType = "application/x-www-form-urlencoded";
objRequest2.Timeout = 100000;
objRequest2.CookieContainer = new CookieContainer();
objRequest2.CookieContainer.Add(objResponse1.Cookies);
myWriter = new StreamWriter(objRequest2.GetRequestStream());
myWriter.Write(postData);

objResponse2 = (HttpWebResponse)objRequest2.GetResponse();
sr = new StreamReader(objResponse2.GetResponseStream());
result = sr.ReadToEnd();
sr.Close();

I am hoping this 2nd request trigger the postback and therefore the
output data in the 2nd panel but it does not. What am I doing wrong?

Cut and pasting myself again...

Your requested Content-Length will *never* match the actual
Content-Length. That's because you use a StreamWriter constructor that
implicitly uses UTF-8 encoding (good) but prepends the UTF-8 byte order
mark at the beginning of the transmitted string (bad). It gets worse if
"postData " contains non 7-bit characters such as a German umlaut. These
characters may take two ore more bytes to encode. The harmless string
Name=Jörg will take 3 (BOM) + 9 (string length) + 1 (additonal byte to
encode the 'ö') = 13 bytes to send, but your code will say it's only 9
bytes.

Morale of the story: Encode the string yourself using an appropriate
encoding object, measure the resulting byte array's size (that's your
real Content-Length) and write it directly to the request stream:

// This constructor supresses the BOM
Encoding utf8 = new UTF8Encoding();
// Encode string manually
byte[] content = utf8.GetBytes(strPost);
// Set header
request.ContentLength = content.Length;

// Now, use Stream.Write to write the bytes to the request stream
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(content, 0, content.Length);
}

Your code also neither explictly calls Close() nor Flush() on the
writer.

Cheers,
 
T

TJO

Thanks Joerg,
I have added the code you suggested but my same problem persists. Why is my
second request not initiating the Postback to my target web form?


Joerg Jooss said:
"TJO" spoke:
I am working with the HttpWebRequest agains a simple aspx page that I
created. The page has two panels one for the input of two html
input boxes and the secone panel that displays the values of the
input boxes on postback.

I make my first web GET request like this:
objRequest1 = (HttpWebRequest)System.Net.HttpWebRequest.Create(uri);
objRequest1.Method = "GET";
objRequest1.ContentType = "text/html";
objRequest1.CookieContainer = new CookieContainer();
objRequest1.AllowAutoRedirect = true;

objResponse1 = (HttpWebResponse)objRequest1.GetResponse();
sr = new StreamReader(objResponse1.GetResponseStream());
result = sr.ReadToEnd();
sr.Close();

I then receive a session cookie that I put in my 2nd request like
this:

string postData = "txt_name=tim&txt_address=261 Trailway"

objRequest2 = (HttpWebRequest)System.Net.HttpWebRequest.Create(uri);
objRequest2.Method = "POST";
objRequest2.AllowAutoRedirect = true;
objRequest2.ContentLength = postData.Length;
objRequest2.ContentType = "application/x-www-form-urlencoded";
objRequest2.Timeout = 100000;
objRequest2.CookieContainer = new CookieContainer();
objRequest2.CookieContainer.Add(objResponse1.Cookies);
myWriter = new StreamWriter(objRequest2.GetRequestStream());
myWriter.Write(postData);

objResponse2 = (HttpWebResponse)objRequest2.GetResponse();
sr = new StreamReader(objResponse2.GetResponseStream());
result = sr.ReadToEnd();
sr.Close();

I am hoping this 2nd request trigger the postback and therefore the
output data in the 2nd panel but it does not. What am I doing wrong?

Cut and pasting myself again...

Your requested Content-Length will *never* match the actual
Content-Length. That's because you use a StreamWriter constructor that
implicitly uses UTF-8 encoding (good) but prepends the UTF-8 byte order
mark at the beginning of the transmitted string (bad). It gets worse if
"postData " contains non 7-bit characters such as a German umlaut. These
characters may take two ore more bytes to encode. The harmless string
Name=Jörg will take 3 (BOM) + 9 (string length) + 1 (additonal byte to
encode the 'ö') = 13 bytes to send, but your code will say it's only 9
bytes.

Morale of the story: Encode the string yourself using an appropriate
encoding object, measure the resulting byte array's size (that's your
real Content-Length) and write it directly to the request stream:

// This constructor supresses the BOM
Encoding utf8 = new UTF8Encoding();
// Encode string manually
byte[] content = utf8.GetBytes(strPost);
// Set header
request.ContentLength = content.Length;

// Now, use Stream.Write to write the bytes to the request stream
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(content, 0, content.Length);
}

Your code also neither explictly calls Close() nor Flush() on the
writer.

Cheers,
 
J

Joerg Jooss

"TJO" spoke:
Thanks Joerg,
I have added the code you suggested but my same problem persists.
Why is my second request not initiating the Postback to my target
web form?

Hm, if that is a ASP.NET web form, you probably need to include some
more information in the request like the pressed button and the
viewstate.

Cheers,
 
Joined
Mar 7, 2007
Messages
1
Reaction score
0
The Solution is Here

Hello Guys ,

The ASPX Page realizes the PostBack with the Help of the Three Parameters along with the Post back values that u want to submit to the ASPX Page.

For the ASPX Page to recognize the PostBack , first it checks for the Valid ViewState of that Page.

Step1: So guys get the ViewState of the Page at the time of the Page Load using the Get Method

You will need to provide the ViewState as a PostBack Parameter as __VIEWSTATE=viewState(that u just got).

The ASPX Page now requires the Two more parameters to know what trigerred the PostBack.
The first Parameter is the __EVENTTARGET that takes the ID of the Control that trigerred the PostBack and the __EVENTARGUMENT that has the argument values of the Control


Step2: U need to provide the values og the __EVENTTARGET and the __EVENTARGUMENT externally in the PostBack data string

So the final string that u write using the StreamWriter will look like this

string PostBack = String.Format("__VIEWSTATE={0}&__EVENTTARGET={1}&__EVENTARGUMENT={2}&username=asdf&password=12345",viewState,ControlID,"");

This will surely raise the Post Back at the ASPX Page.
 

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