473,508 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to I do a http post from within a webform

I have a webform, from which I have to submit info to another site. Their
instructions are to have a html form, with the following as the submit:
<form method="post" action="http://www.theirsite.htm">
<input type=hidden name="field1" value="value1">
<input type=hidden name="field2> value="value2>
<input type="submit" value="Click Me">

On my web form, I have numerous web controls ( all runat="server"), one of
which is a button which the user must click to prepare to move to the next
site. I would like to add code to this button to simulate the effect of the
form above. How do I do this? I see how I can post the data to the target
site, with say a WebClient, or HttpRequest write, but that doesn't take me
to the new page. I could transfer to the new page, but it doesn't submit
the required info. I could add a html control to the page, but this
wouldn't have access to the web controls, and hence none of the info
collected there.

Thanks in advance

--
Phillip N Rounds
The Cassandra Group, Inc
68 Sand Hill Rd
Flemington NJ 08822
pr*****@cassandragroup.com
Jul 21 '05 #1
6 2120
Go ahead and use the HTML controls if that makes your life easier. You can
right click on them and select "Run as server control" so they'll be visible
from your server side code.
When the time comes to submit to the other web site, I'd suggest client side
javascript. document.Form1.Submit()

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"Phillip N Rounds" <pr*****@cassandragroup.com> wrote in message
news:OQ**************@TK2MSFTNGP12.phx.gbl...
I have a webform, from which I have to submit info to another site. Their
instructions are to have a html form, with the following as the submit:
<form method="post" action="http://www.theirsite.htm">
<input type=hidden name="field1" value="value1">
<input type=hidden name="field2> value="value2>
<input type="submit" value="Click Me">

On my web form, I have numerous web controls ( all runat="server"), one
of
which is a button which the user must click to prepare to move to the next
site. I would like to add code to this button to simulate the effect of
the
form above. How do I do this? I see how I can post the data to the
target
site, with say a WebClient, or HttpRequest write, but that doesn't take me
to the new page. I could transfer to the new page, but it doesn't submit
the required info. I could add a html control to the page, but this
wouldn't have access to the web controls, and hence none of the info
collected there.

Thanks in advance

--
Phillip N Rounds
The Cassandra Group, Inc
68 Sand Hill Rd
Flemington NJ 08822
pr*****@cassandragroup.com

Jul 21 '05 #2
I found one way to do something pretty similar. If you find a better way,
pl. let me know.

In this example WebForm1.aspx goes to WebForm2.aspx via http post.
<code>
********************WebForm1.aspx*****************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Text;

namespace WebApplication5
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
WebRequest myRequest =
WebRequest.Create("http://localhost/WebApplication5/WebForm2.aspx");
myRequest.Headers["field1"]="value1";
myRequest.Headers["field2"]="value2";
myRequest.Method="POST";
myRequest.ContentType="text/html";
myRequest.ContentLength=0;
WebResponse resp = myRequest.GetResponse();
Stream ReceiveStream = resp.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader( ReceiveStream, encode );
string str = readStream.ReadToEnd();
readStream.Close();
resp.Close();
Response.Write(str);
Response.Flush();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
********************WebForm2.aspx*****************
public class WebForm2 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
string field1=(string)Request.Headers["field1"];
string field2=(string)Request.Headers["field2"];
Response.Write("Your value for field1 was " + field1 + " & field2 was " +
field2);
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}

</code>
"Phillip N Rounds" wrote:
I have a webform, from which I have to submit info to another site. Their
instructions are to have a html form, with the following as the submit:
<form method="post" action="http://www.theirsite.htm">
<input type=hidden name="field1" value="value1">
<input type=hidden name="field2> value="value2>
<input type="submit" value="Click Me">

On my web form, I have numerous web controls ( all runat="server"), one of
which is a button which the user must click to prepare to move to the next
site. I would like to add code to this button to simulate the effect of the
form above. How do I do this? I see how I can post the data to the target
site, with say a WebClient, or HttpRequest write, but that doesn't take me
to the new page. I could transfer to the new page, but it doesn't submit
the required info. I could add a html control to the page, but this
wouldn't have access to the web controls, and hence none of the info
collected there.

Thanks in advance

--
Phillip N Rounds
The Cassandra Group, Inc
68 Sand Hill Rd
Flemington NJ 08822
pr*****@cassandragroup.com

Jul 21 '05 #3
I would rather not do it that way ( besides, I don't know how to access
values contained in my web controls, and variables I have calculated in th
background from the client side java script, or, conversely, to invoke
client side functions from within the web controls, but that is another
story.)
I have one web control button which has to be invoked to finalize
calcuations on the webform, and it has to be a webform control not a html
control. It seems bad design to have to have a second control to finish the
processing.

From another source, I have tried the following

HttpResponse myResponse = HttpContext.CurrentResponse;
MyResponse.ClearContents();
MyResponse.ClearHeaders();
MyResponse.Output.Flush();
String NewHTML = "<html><body><form name='form1' method='post'
action='http://www.newwebsite.com'>
<input name='field1' type='hidden'
value='abcdefg'>
<input name='field2.... > .... </form>
<script language=javascript>
document.redirect.submit();></script></body></html>";

char[] output = NewHTML.ToCharArray();
MyResponse.Write( outout, 0, output.Length);

This takes me to the appropriate site, and posts all my variables
appropriately. My problem here is that while at the new site, if the user
hits the back button on the browser, it gets sent to the page I just
overwrote, which redirects the user back to the target site.

Thanks for the input
"Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Go ahead and use the HTML controls if that makes your life easier. You can right click on them and select "Run as server control" so they'll be visible from your server side code.
When the time comes to submit to the other web site, I'd suggest client side javascript. document.Form1.Submit()

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"Phillip N Rounds" <pr*****@cassandragroup.com> wrote in message
news:OQ**************@TK2MSFTNGP12.phx.gbl...
I have a webform, from which I have to submit info to another site. Their instructions are to have a html form, with the following as the submit:
<form method="post" action="http://www.theirsite.htm">
<input type=hidden name="field1" value="value1">
<input type=hidden name="field2> value="value2>
<input type="submit" value="Click Me">

On my web form, I have numerous web controls ( all runat="server"), one
of
which is a button which the user must click to prepare to move to the next site. I would like to add code to this button to simulate the effect of
the
form above. How do I do this? I see how I can post the data to the
target
site, with say a WebClient, or HttpRequest write, but that doesn't take me to the new page. I could transfer to the new page, but it doesn't submit the required info. I could add a html control to the page, but this
wouldn't have access to the web controls, and hence none of the info
collected there.

Thanks in advance

--
Phillip N Rounds
The Cassandra Group, Inc
68 Sand Hill Rd
Flemington NJ 08822
pr*****@cassandragroup.com


Jul 21 '05 #4
This doesn't work in my case. The key here is that the web request doesn't
recognize https, which is where I'm going

From another source, I have tried the following

HttpResponse myResponse = HttpContext.CurrentResponse;
MyResponse.ClearContents();
MyResponse.ClearHeaders();
MyResponse.Output.Flush();
String NewHTML = "<html><body><form name='form1' method='post'
action='http://www.newwebsite.com'>
<input name='field1' type='hidden'
value='abcdefg'>
<input name='field2.... > .... </form>
<script language=javascript>
document.redirect.submit();></script></body></html>";

char[] output = NewHTML.ToCharArray();
MyResponse.Write( outout, 0, output.Length);

This takes me to the appropriate site, and posts all my variables
appropriately. My problem here is that while at the new site, if the user
hits the back button on the browser, it gets sent to the page I just
overwrote, which redirects the user back to the target site.

Any suggestions as to how I can get around that would be appreciated.

Thanks for the input
"santanbo" <sa******@discussions.microsoft.com> wrote in message
news:3F**********************************@microsof t.com...
I found one way to do something pretty similar. If you find a better way,
pl. let me know.

In this example WebForm1.aspx goes to WebForm2.aspx via http post.
<code>
********************WebForm1.aspx*****************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Text;

namespace WebApplication5
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
WebRequest myRequest =
WebRequest.Create("http://localhost/WebApplication5/WebForm2.aspx");
myRequest.Headers["field1"]="value1";
myRequest.Headers["field2"]="value2";
myRequest.Method="POST";
myRequest.ContentType="text/html";
myRequest.ContentLength=0;
WebResponse resp = myRequest.GetResponse();
Stream ReceiveStream = resp.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader( ReceiveStream, encode );
string str = readStream.ReadToEnd();
readStream.Close();
resp.Close();
Response.Write(str);
Response.Flush();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
********************WebForm2.aspx*****************
public class WebForm2 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
string field1=(string)Request.Headers["field1"];
string field2=(string)Request.Headers["field2"];
Response.Write("Your value for field1 was " + field1 + " & field2 was " +
field2);
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}

</code>
"Phillip N Rounds" wrote:
I have a webform, from which I have to submit info to another site. Their instructions are to have a html form, with the following as the submit:
<form method="post" action="http://www.theirsite.htm">
<input type=hidden name="field1" value="value1">
<input type=hidden name="field2> value="value2>
<input type="submit" value="Click Me">

On my web form, I have numerous web controls ( all runat="server"), one of which is a button which the user must click to prepare to move to the next site. I would like to add code to this button to simulate the effect of the form above. How do I do this? I see how I can post the data to the target site, with say a WebClient, or HttpRequest write, but that doesn't take me to the new page. I could transfer to the new page, but it doesn't submit the required info. I could add a html control to the page, but this
wouldn't have access to the web controls, and hence none of the info
collected there.

Thanks in advance

--
Phillip N Rounds
The Cassandra Group, Inc
68 Sand Hill Rd
Flemington NJ 08822
pr*****@cassandragroup.com

Jul 21 '05 #5
Hi Philip,
I would rather not do it that way
Which way are you referring here: Steve's or mine?

Santanu

"Phillip N Rounds" wrote:
I would rather not do it that way ( besides, I don't know how to access
values contained in my web controls, and variables I have calculated in th
background from the client side java script, or, conversely, to invoke
client side functions from within the web controls, but that is another
story.)
I have one web control button which has to be invoked to finalize
calcuations on the webform, and it has to be a webform control not a html
control. It seems bad design to have to have a second control to finish the
processing.

From another source, I have tried the following

HttpResponse myResponse = HttpContext.CurrentResponse;
MyResponse.ClearContents();
MyResponse.ClearHeaders();
MyResponse.Output.Flush();
String NewHTML = "<html><body><form name='form1' method='post'
action='http://www.newwebsite.com'>
<input name='field1' type='hidden'
value='abcdefg'>
<input name='field2.... > .... </form>
<script language=javascript>
document.redirect.submit();></script></body></html>";

char[] output = NewHTML.ToCharArray();
MyResponse.Write( outout, 0, output.Length);

This takes me to the appropriate site, and posts all my variables
appropriately. My problem here is that while at the new site, if the user
hits the back button on the browser, it gets sent to the page I just
overwrote, which redirects the user back to the target site.

Thanks for the input
"Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Go ahead and use the HTML controls if that makes your life easier. You

can
right click on them and select "Run as server control" so they'll be

visible
from your server side code.
When the time comes to submit to the other web site, I'd suggest client

side
javascript. document.Form1.Submit()

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"Phillip N Rounds" <pr*****@cassandragroup.com> wrote in message
news:OQ**************@TK2MSFTNGP12.phx.gbl...
I have a webform, from which I have to submit info to another site. Their instructions are to have a html form, with the following as the submit:
<form method="post" action="http://www.theirsite.htm">
<input type=hidden name="field1" value="value1">
<input type=hidden name="field2> value="value2>
<input type="submit" value="Click Me">

On my web form, I have numerous web controls ( all runat="server"), one
of
which is a button which the user must click to prepare to move to the next site. I would like to add code to this button to simulate the effect of
the
form above. How do I do this? I see how I can post the data to the
target
site, with say a WebClient, or HttpRequest write, but that doesn't take me to the new page. I could transfer to the new page, but it doesn't submit the required info. I could add a html control to the page, but this
wouldn't have access to the web controls, and hence none of the info
collected there.

Thanks in advance

--
Phillip N Rounds
The Cassandra Group, Inc
68 Sand Hill Rd
Flemington NJ 08822
pr*****@cassandragroup.com



Jul 21 '05 #6
I think document.history.back() in your html string is worth exploring. I
don't know whether it will work as I haven't tried it in this context.

Let me know..

"Phillip N Rounds" wrote:
This doesn't work in my case. The key here is that the web request doesn't
recognize https, which is where I'm going

From another source, I have tried the following

HttpResponse myResponse = HttpContext.CurrentResponse;
MyResponse.ClearContents();
MyResponse.ClearHeaders();
MyResponse.Output.Flush();
String NewHTML = "<html><body><form name='form1' method='post'
action='http://www.newwebsite.com'>
<input name='field1' type='hidden'
value='abcdefg'>
<input name='field2.... > .... </form>
<script language=javascript>
document.redirect.submit();></script></body></html>";

char[] output = NewHTML.ToCharArray();
MyResponse.Write( outout, 0, output.Length);

This takes me to the appropriate site, and posts all my variables
appropriately. My problem here is that while at the new site, if the user
hits the back button on the browser, it gets sent to the page I just
overwrote, which redirects the user back to the target site.

Any suggestions as to how I can get around that would be appreciated.

Thanks for the input
"santanbo" <sa******@discussions.microsoft.com> wrote in message
news:3F**********************************@microsof t.com...
I found one way to do something pretty similar. If you find a better way,
pl. let me know.

In this example WebForm1.aspx goes to WebForm2.aspx via http post.
<code>
********************WebForm1.aspx*****************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Text;

namespace WebApplication5
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
WebRequest myRequest =
WebRequest.Create("http://localhost/WebApplication5/WebForm2.aspx");
myRequest.Headers["field1"]="value1";
myRequest.Headers["field2"]="value2";
myRequest.Method="POST";
myRequest.ContentType="text/html";
myRequest.ContentLength=0;
WebResponse resp = myRequest.GetResponse();
Stream ReceiveStream = resp.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader( ReceiveStream, encode );
string str = readStream.ReadToEnd();
readStream.Close();
resp.Close();
Response.Write(str);
Response.Flush();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
********************WebForm2.aspx*****************
public class WebForm2 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
string field1=(string)Request.Headers["field1"];
string field2=(string)Request.Headers["field2"];
Response.Write("Your value for field1 was " + field1 + " & field2 was " +
field2);
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}

</code>
"Phillip N Rounds" wrote:
I have a webform, from which I have to submit info to another site. Their instructions are to have a html form, with the following as the submit:
<form method="post" action="http://www.theirsite.htm">
<input type=hidden name="field1" value="value1">
<input type=hidden name="field2> value="value2>
<input type="submit" value="Click Me">

On my web form, I have numerous web controls ( all runat="server"), one of which is a button which the user must click to prepare to move to the next site. I would like to add code to this button to simulate the effect of the form above. How do I do this? I see how I can post the data to the target site, with say a WebClient, or HttpRequest write, but that doesn't take me to the new page. I could transfer to the new page, but it doesn't submit the required info. I could add a html control to the page, but this
wouldn't have access to the web controls, and hence none of the info
collected there.

Thanks in advance

--
Phillip N Rounds
The Cassandra Group, Inc
68 Sand Hill Rd
Flemington NJ 08822
pr*****@cassandragroup.com


Jul 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1113
by: Phillip N Rounds | last post by:
I have a webform, from which I have to submit info to another site. Their instructions are to have a html form, with the following as the submit: <form method="post"...
7
1220
by: Thomas Kirk | last post by:
I was wondering how I would do a form post from one ASP.Net webform to another webform in another ASP.Net application. I have tried this using the httprequest object but the 2nd webform is never...
1
1025
by: quinton | last post by:
I have a webform with a custom server control applied to it. on the webform I have a single server side textbox which I wish to access from within the server side control after submiting the form...
9
2108
by: WJ | last post by:
I have an Asp.Net page that redirects the user to another page with a piece of data to be passed. Below is the c# instruction: Response.Redirect("mySpecialPage.aspx?go1="+strGoName); This will...
2
1449
by: Brad Mann | last post by:
I have a need to get the html (in the form of a string) for a rendered webform so that it can be used in the body of an e-mail. How is this done? The code will be executed from a server control...
0
1569
by: mike | last post by:
How do i perform a databind on a web user control within a repeater or rather how can I access the datasource that is already bound? I have a web user control that displays a table of values (the...
1
4243
by: David | last post by:
I need to redirect to a page and HTTP Post data. The Response.Redirect does not work and the HTTPREQUEST option calls the page and waits for a response, but I need to transfer control to the...
8
5064
by: Gert | last post by:
Hi, I have a form (server side) because of the filling of variables through the application. But now I need to post it to an url on submit. My .HTML form looks like this, but how to translate it...
8
2036
by: vinesh | last post by:
I have sample Asp.Net Web Application project. Let me know how to keep the files related to this project (like the webform.aspx, WebForm1.aspx.vb, WebForm1.aspx.resx) in a separate folder within a...
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7333
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.