HttpWebRequest

G

Guest

Hi

I have been trying to post data to a form on a web page using methods within the system.net namespace. I have followed all the documentation to the letter, but I just can't seem to post the form. The form has an event written in c# called Button1_Click. It seems that this event is not firing when the data is posted to the web form. The button is added to the page using <asp:Button id="Button1" runat="server" Text="Button1" OnClick="Button1_Click" /> The code used to submit the form is as follows

//encode the form data string into a byte arra
byte[] buffer = Encoding.ASCII.GetBytes(postData)
//Create an instance of a HttpRequest objec
HttpWebRequest objReq = (HttpWebRequest)WebRequest.Create(strURL)

//indicate that we will be posting the dat
hwreq.Method="POST"
hwreq.ContentType="application/x-www-form-urlencoded"

//send the for
objReq.AllowAutoRedirect=true
objReq.KeepAlive=false
objReq.ContentLength=buffer.Length
Stream objReqStream = objReq.GetRequestStream()

objStream.Write(buffer,0,buffer.Length)
HttpWebResponse objResp = (HttpWebResponse)objReq.GetResponse()

Stream objRespStream = objResp.GetResponseStream()
StreamReader sr = new StreamReader(objRespStream,Encoding.ASCII)
string strResult=sr.ReadToEnd()

The posted data (converted to the buffer above) contains the following string "TextBox1=Glenn Wilson&TextBox2=Developer

Does anyone know why my code appears not to work? It should be simple

Glenn
 
G

Guest

Glenn
The button should have the event OnServerClick="Button1_Click

Tu-Thac

----- Glenn Wilson wrote: ----

Hi

I have been trying to post data to a form on a web page using methods within the system.net namespace. I have followed all the documentation to the letter, but I just can't seem to post the form. The form has an event written in c# called Button1_Click. It seems that this event is not firing when the data is posted to the web form. The button is added to the page using <asp:Button id="Button1" runat="server" Text="Button1" OnClick="Button1_Click" /> The code used to submit the form is as follows

//encode the form data string into a byte arra
byte[] buffer = Encoding.ASCII.GetBytes(postData)
//Create an instance of a HttpRequest objec
HttpWebRequest objReq = (HttpWebRequest)WebRequest.Create(strURL)

//indicate that we will be posting the dat
hwreq.Method="POST"
hwreq.ContentType="application/x-www-form-urlencoded"

//send the for
objReq.AllowAutoRedirect=true
objReq.KeepAlive=false
objReq.ContentLength=buffer.Length
Stream objReqStream = objReq.GetRequestStream()

objStream.Write(buffer,0,buffer.Length)
HttpWebResponse objResp = (HttpWebResponse)objReq.GetResponse()

Stream objRespStream = objResp.GetResponseStream()
StreamReader sr = new StreamReader(objRespStream,Encoding.ASCII)
string strResult=sr.ReadToEnd()

The posted data (converted to the buffer above) contains the following string "TextBox1=Glenn Wilson&TextBox2=Developer

Does anyone know why my code appears not to work? It should be simple

Glenn
 
G

Guest

I am not sure I understand the reasoning behind this. The button has the runat="server" property, so I would assume that the button's event would fire on the server. Indeed, when I tried this, the application still failed to fire the event.

I forgot to mention in the original mail that the web page works fine if I navigate to it via a browser. It only fails when I try to post a request from an application. The connection to the page is working, since I can return a view of the requested page. The only problem is, the button's OnClick event doesn't appear to work. My hunch is that there is a problem in the code used to post to the page, not in the page itself.

Glenn

----- Tu-Thach wrote: -----

Glenn,
The button should have the event OnServerClick="Button1_Click"

Tu-Thach
 
C

Cor

Hi Glen,

I think that there are more reasons why the code is not working so review
it.

But as Tu-Thach stated it needs a button event (a procedure that handles
what happens)

If you are using C# you have to set that using an handler that tells which
procedure is for that event.

Than you can make a procedure, that handles your event.

My advice is to look in your documentation again how to make that.

I hope this helps,

Cor
 
G

Guest

Hi

I already have a procedure within my aspx page that handles the event

<script language="C#" runat="server"
void Button1_Click(Object Sender, EventArgs e

Label1.Text = "Hello " + TextBox1.Text

</script

It fires when I physically click on the button from within the browser, but does not fire when I post to the page from my application. The reason I know this is that I have written a snippet of code that writes to the event log if the event fires (not shown above)

So, my assumption is that the application posting to the page is causing some problem

Glen


----- Cor wrote: ----

Hi Glen

I think that there are more reasons why the code is not working so revie
it

But as Tu-Thach stated it needs a button event (a procedure that handle
what happens

If you are using C# you have to set that using an handler that tells whic
procedure is for that event

Than you can make a procedure, that handles your event

My advice is to look in your documentation again how to make that

I hope this helps

Co
 
C

Cor

Hi Glenn,

Now I think I start to understand what you want to archieve, it looks to me
if you want to post data to a webpage on the user client. I think that you
cannot do.

You can make a new page, and show that to the user by using a redirect or
changing the page that he is using, but not send some data to that page (In
fact it is the same because the user will get only the new things, the rest
is normaly in the cache from his computer).

I am not saying it does not exist, but I never heard of it.

Cor
 

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