Problem Programatically Posting to an ASPX PAge

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello All:

I cannot seem to get a simple programmatic post to work. From one ASPX
page, I want to generate HTML from another page. Thus, I programatically
post to the second page and read the response. However, my posted data is
always null in the second page. Can anyone tell me why this doesn't work?


HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
"http://localhost/page2.aspx");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string dataToPost = "name=Goober";
byte [] bytes = System.Text.Encoding.ASCII.GetBytes(dataToPost);
request.ContentLength = bytes.Length;
System.IO.Stream request_stream = request.GetRequestStream();
request_stream.Write(bytes, 0, bytes.Length);
request_stream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.IO.Stream response_stream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader
(response_stream);
 
Robert,
I don't see anything wrong with your code. Why not try using the simplified
WebClient.UploadData method? Put a breakpoint in the Page_Load handler of the
receiving page and examine the Request.Form or Request.Params collections.
Peter
 
Back
Top