I have an html form that looks like this:
<html><head> </head><body><form enctype="multipart/form-data"
method="POST"
action="http://externalsite.com/process"><input type="text" name="text"><input type="submit" value="OK"/></form></body></html>When I click OK, the browser goes to http://externalsite.com/process and I see the results of the computation on the text.
My .aspx fiIe contains this form (without the action attribute) and an <asp:button>. I need to process the text and then send the output to the external site. If I set the button's .PostBackUrl to http://externalsite.com/process, the button's OnClick event never fires, so I do not get to process the text before ending it.
I tried to use an HttpWebRequest with code like this:
byte[] data = BuildMultiPartFormData();
// Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://externalsite.com/process");
myRequest.AllowAutoRedirect = true;
myRequest.Method = "POST";
myRequest.ContentType = String.Format("multipart/form-data, boundary={0}", border);
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
Response.Clear();
StreamReader response = new StreamReader(myRequest.GetResponse().GetResponseStream());
string buffer = response.ReadToEnd();
Response.Write(buffer);This almost works, but:
1) The page is incomplete. Also, when I look at the page source I see:
<html>
<!-- code for new page -->
</html>
<html>
<!-- codefor my page -->
</html>
2) The browser is not redirected to the external website.
I should also point out that the response from the external website is:
HTTP/1.1 200 OK\r\n ... Content-Type: application/xhtml+xml;charset=UTF-8\r\n Transfer-Encoding: chunked\r\n\r\n ...
Do I need to de-chunk the data?
What is the simplest way to do what I want, which is simply to process some data on my page before posting the processed data to an external site for further processing?