Post and redirect

L

Laith Zraikat

I am trying to invoke a post request from code behind of an asp.net page
using "WebClient" object, and I want the user to be redirected to the action
url as well.

So far Ive been able to send the post and get the response. using this code:

Dim PostData as string
Dim ActionURL as string
Dim myWebClient As WebClient = New WebClient

ActionURL = ("https://www.mysite.com/post.cgi")

postData = ("param1=" & param1& "&param2=" & param2)

myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

Dim byteArray As Byte() = Encoding.ASCII.GetBytes(postData)

Dim responseArray As Byte() = myWebClient.UploadData(CUURL, "POST", byteArray)

HttpContext.Current.Response.Write(Replace(System.Text.Encoding.ASCII.GetString(responseArray))
HttpContext.Current.Response.End()

How can I perform a post which will also send the user to the "ActionURL"
displaying the results of the post? i.e. I want it to behave as if it were
a normal form submit from the browser.

Thank you for your time.
 
W

Walter Wang [MSFT]

Hi Laith,

Is the ActionURL on the same domain as your current page?

I don't think it's possible, when you send the user to the ActionURL, you
need to do a redirect. However, doing a redirect will cause a new request
to the ActionURL, the previously posted data will not be seen by this new
request.

If the ActionURL is on the same domain as your current page and is also an
ASP.NET page, then you may use Server.Transfer to transfer the execution to
the ActionURL. For more information, please refer to:

#Passing Server Control Values Between Pages
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconpassingservercontrolvaluesbetweenpages.asp

Hope this helps. Please feel free to post here if anything is unclear.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeeran

Hello Walter Wang [MSFT],

Thank you for the quick response.

The ActionURL is on a different server. Basically what I am trying to do
it integrate with a payment gateway provider that requires parameters to
be passed using POST.
 
W

Walter Wang [MSFT]

Hi Jeeran,

You can use a second form on the page - below the Web form server form -
with client side script performing the submit and post.

<form method="POST" action="https://www.mysite.com/post.cgi">
<input type="hidden" name="LOGIN" value="Your LOGIN here">
<input type="hidden" name="AMOUNT" value="AMOUNT here">
<input type="hidden" name="TYPE" value="TYPE here">
<input type="submit" value="Click here to Purchase">
</form>

However, the fields need to be posted must have name defined.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeeran

Hello Walter Wang [MSFT],

True, that would work. Howerver, I need to perform some actions in the code-behind
before sending the data to the action url. I thought of sending an AJAX callback
to my page in order to perform this action, before submitting the form, but
this will force me to expose certain information about this action in my
client-side code, so I didn't. Plus I don't beleive AJAX should be used for
such purposes.

Using the code I submitted earlier, I managed to write the response html
onto my page. My users then have to submit an ID number and password associated
with their account with the payment gateway -it is something like paypal-
which works fine.

My only concern is that my users will perceive it a security risk that the
page from the payment gateway appears as thought it is coming from my site.

A middle solution might be to have the page do a normal postback, perform
whatever actions I want in the back end, then send the user to an empty page
with the hidden form, and have the page auto submit "Onload". While this
will work, I feel it is an out-dated solution and that there must be a better
solution out there.

Your input is highly appreciated.

Thank you for your time.
 
S

Steven Cheng[MSFT]

Thanks for your followup Jeeran,

Walter has been absent due to some urgent issue and I'll help him work on
this issue.

From your last reply, my understanding on this issue is that you have the
following two concerns correctly:

1. The data you collected will be posted to a remote site and you do not
want the data to be exposed in url querystring or any client-side script
(AJAX)

2. You will need to perform some server-side processing before submit the
data to the remote site.


Regarding on this , I'm wondering the following points:

1) Is the server-side processing (in code-behind) related to the data you
submit to the remote site or will the processing result affect the
submiting?

2) Do you think it convenient or doable if we first submit to the same
form(main form) and then register some client-side script to resubmit the
page to remote site after postback?


so far, based on my research, there are two potential solutions:

1. In ASP.NET 2.0, the webform support cross-page postback, thus, we can
add a button on the web form and set its "PostBackUrl" to the remote
site(and use css style to hide it). Then, in a normal submit's postback
event, we do those necessary server-side processing in code behind and
programmtically register some scripts to invoke that Button(whose
"PostBackUrl" is pointing to the remote site) 's "click" method

#Cross-Page Posting in ASP.NET Web Pages
http://msdn2.microsoft.com/en-us/library/ms178139.aspx


2. Still use an additional form to post to remote site. We can make it
invisible through css style. And in our main form, we add a submit button
which will postback to do those necessary server-side tasks and then
programmtically register client-side script to submit that additinal hidden
form. Also, in thise case, we need to programmtically copy the data from
the textboxes in main form into the textbox(<input type="text" ..../>) in
the additional form.

Here is a test page which demonstrate both of the above two approahes(I've
also attached the complete test page, aspx and codebehind cs file in this
message, you can get it if you are using Outlook express to visit the
newsgroup):


===========aspx ===============
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" >
function submit_remoteform()
{
document.forms["remoteform"].submit();

}
</script>
</head>
<body>

<form id="remoteform" action="targetpage.aspx" method="post"
style="display:none">
Remote Form<br /><hr />
&nbsp;<br />
<input id="txt1" name="txt1" type="text" value='<%= txt1.Text %>' />
<input id="txt2" name="txt2" type="text" value='<%= txt2.Text %>'/>
</form>

<form id="form1" runat="server">
<div>
Main form<br /><hr />
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>

<asp:Button ID="btnSubmit" runat="server" Text="Submit Through Main
Form" OnClick="btnSubmit_Click" />



<asp:Button ID="Button2" runat="server"
PostBackUrl="TargetPage.aspx" Text="Button" style="display:none" /><br />


<asp:Button ID="Button1" runat="server" Text="Submit Through
Additional Form" OnClick="Button1_Click" />


<br />
<br />
<br />


</div>
</form>

</body>

</html>
==================================

========code behind======================
public partial class ClientSide_SourcePage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Response.Write("<br/>btnSubmit_Click....." +
DateTime.Now.ToLongTimeString());


Page.ClientScript.RegisterStartupScript(this.GetType(),
"post_main_form",
"document.getElementById('Button2').click();", true);
}




protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<br/>Button1_Click....." +
DateTime.Now.ToLongTimeString());

Page.ClientScript.RegisterStartupScript(this.GetType(),
"post_main_form",
"submit_remoteform();", true);
}
}

===========================================

Please feel free to let me know if there is anything unclear or any other
information you wonder.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

Hello Jeeran,

How are you doing on this issue or have you got any further progress? If
you still have any questions on this or any other information you wonder,
please feel free to let me know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeeran

Hello Steven Cheng[MSFT],

What I ended up doing is use the the way I described in my first post. the
problem I was having before with this method is that in firefox, I get the
html output printed on the page, and not rendered by the browser. But in
IE it worked fine.

I tried the additonal form technique and discovered that the same problem
still happens in FF, which means it was not because of the first technique
I was using, so I reverted back to it and all is working great now in IE.
The FF problem is now being fixed from the payment gateway provider's side.

Thanks for all your help and follow-up.
 
G

Guest

Dear Walter,

your replies was useful for me for the idea of post a form from your client
to the targeted page.

my problem is,i have an asp page,i want from an aspx page to post the
username ans password for this page,and to get the response of it.

can you send me a piece of code to do that.....i read alot of treads about
httpWebRequest and how to specify the POST method in it,and pass the
parameter,this is work fine if the page accept the for authentication the
Quesry string ??? or i this is the only way to post data to other page using
HTTP POST.

the HTML for the targeted page is
--------------------------------------------------------------------------------------------
<tr>
<td height="158" align="center">
<p><font face="arial" size="1"><b>Customer Number:</b></font><br>
<input type="TEXT" name="USER_ID" size="16" border="0">
<br>
<font face="arial" size="1"><b>Customer Password:</b></font><br>
<input type="PASSWORD" name="PASSWORD" size="16" border="0">
</p>
<p>
<input name="SUBMIT" type="SUBMIT" value="Login" border="0">
<br>
<br>
</p>
</td>
</tr
----------------------------------------------------------------------------------------
this means by using HTTP POST i have to pass the following parameters :
USER_ID , PASSWORD
and how can i submit the form..................... ???!!!!

if you have any sample code for that........this will be great.

please reply ASAP.
 

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

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top