Click to See Complete Forum and Search --> : How do I redirect to another page using POST method


thabang
July 3rd, 2008, 05:25 AM
Hi

I'm in a particularly funny situation. The webpage I want to redirect to will only open successfully if I redirect to it using only POST method.

I battled using <form ../>, but apparently that seems to just be refreshing the page (No, I'm not that much of a newbie, I do not specify the same URL in the action tag as the page I'm in), I think its the DotNetNuke framework I'm using, either way, <form..> doesn't work, so I cant redirect using that


<form method="POST" action="http://www.google.co.za/search">
<input type="hidden" name="hl" value="en">
<input type="hidden" name="q" value="redirect+post+method">
<input type="submit" value="Search using google">
</form>


The only successful redirecting I managed to pull of is Response.Redirect(URL), but this transmits under the GET method.

The example code I found from the web just seems to not pulling it off too, like:

'Server.Transfer - only redirects to local pages (child pages)
Server.Transfer(URL)

'And this
Dim hRequest As HttpWebRequest = WebRequest.Create("http://www.google.co.za/search")
hRequest.Method = "POST"
hRequest.ContentType = "application/x-www-form-urlencoded"
Dim sRequestStream As Stream = hRequest.GetRequestStream()
Dim postBytes As Byte()
Dim enc As New System.Text.ASCIIEncoding
postBytes = enc.GetBytes(URL)
sRequestStream.Write(postBytes, 0, postBytes.Length)
sRequestStream.Close()
'This one is failing too..



Pls help, any input is valued

Regards

HanneSThEGreaT
July 3rd, 2008, 05:46 AM
Hello Thabang! :wave:

Not sure I understand you completely.

Do you want to open up google with the keywords "redirect+post+method" as well as display the reults ¿

Because if I do this :
<form method="GET" action="http://www.google.co.za/search">
<input type="hidden" name="hl" value="en">
<input type="hidden" name="q" value="redirect+post+method">
<input type="submit" value="Search using google">
</form>
It does as I explained.

The POST method of the form is used to send info somewhere, for example, you could use Post to send the results of a form to an email address, or db
If you're going to need to display results based on the form fields, you'd have to use GET

HanneSThEGreaT
July 3rd, 2008, 05:51 AM
BTW, if you want to redirect to another page using the POST method, you're going to need to use JavaScript or even Perl.

Alsvha
July 3rd, 2008, 05:58 AM
AFAIK a HTTP Web Request means you get the stream back and can display it via your context, it doesn't redirect - it simply makes a request, gets you a reply which you can use.

Submitting a normal form however should do the trick, so I find it strange you can't do that?
How do you submit it.
Off the top of my head, I'd say, writing out a form (not an asp.net form) with a name, and then just use javascript to submit that form. It will however mean that the form will have to be submitted from the client, but if you wish to redirect - then well, you must want the client to leave your website.

If not, then as Hannes mentions - we'll need to know what you actually try to accomplish more in detail.

thabang
July 3rd, 2008, 06:13 AM
Hey guys, thanks for responding..

I used google as an example, the site I'm directing to is a paypal site, so the parameters have to be hidden, and that PayPal site does specify it will not work if I use GET.

Refering to the javascript way of submitting a form, that sounds like an option I haven't pursued. I found an example on the net, tried to implement it:

'Heres the form:

<form id='frmExample' method="POST" action="http://www.google.co.za/search">
<input type="hidden" name="hl" value="en">
<input type="hidden" name="q" value="redirect+post+method">
<input type="submit" value="Search using google">
</form>

<script language="javascript" type="text/javascript">
<!--
function RedirectToVCS()
{
document.frmExample.method = "POST";
document.frmExample.submit();
}

-->


my IE returns an error "document.frmExample is null"

Am I doing it right?

thabang
July 3rd, 2008, 06:27 AM
Oh yeh, I have two ways to submit the form..

1. By the submit button in the form, and
2. I have a asp.net button that I put on the form, I added and onclick button, "return RedirectToVCS()"
It does call that function, heres the code for the function:


function RedirectToVCS()
{
document.frmSubmit.method = "POST";
document.frmSubmit.submit();
}

HanneSThEGreaT
July 3rd, 2008, 06:29 AM
You haven't called the JS function :)
You could call the RedirectToVCS from your submit button, something like :
<input type="submit" value="Search using google" onClick="RedirectToVCS()">
That should work

thabang
July 3rd, 2008, 06:48 AM
I just tried that now, that still refreshes the page when I click on the button


<form name="frmSubmit" id="frmSubmit" method="POST" action="https://www.vcs.co.za/" ">
<input type="hidden" name="a1" value="a">
<input type="hidden" name="a2" value="b">
<input type="hidden" name="a3" value="c">
<input type="hidden" name="a4" value="d">
<input type="hidden" name="CardholderEmail" value="p@here.com">
<input type="hidden" name="b1" value="z">
<input type="hidden" name="b2" value="z">
<input type="hidden" name="b3" value="z">
<input type="hidden" name="b4" value="z">
<input type="submit" value="Pay by Credit Card" onclick="redirectToVCS()">
</form>


Is there any other way of doing it, like vb.net maybe?
I've exploere every setting I can find for Response.Redirect, there is no where I can set the POST method. Is there anything else I can try?

GremlinSA
July 3rd, 2008, 12:24 PM
Hey TB ..
I'm seaarching the net and found
"Post-And-Redirect" (http://www.velocityreviews.com/forums/t106243-http-post-and-redirect.html)
Responce redirect (http://www.webmasterworld.com/forum47/1364.htm)
using post & redirect (http://blog.eworldui.net/post/2008/05/ASPNET-MVC---Using-Post2c-Redirect2c-Get-Pattern.aspx)
Extending responce.redirect (http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=432)

I hope the ideas in these help you..

Cheers..

Gremmy...

Alsvha
July 3rd, 2008, 12:58 PM
There must be something strange going on in your code/example - cause I just tried this:

<html><body>
<form name="someForm" action="http://www.google.com" method="post">
</form>
<script>
someForm.submit();
</script>
</body></html>


And when run as an ordinary HTML file, the post submitted me to google.com

My guess is that you somehow get an asp.net form mixed into the equation instead of using a pure html form. Normally when submitting an asp.net form you'll trigger a post back, lest you manipulate the action explicitly.

Try making a very simple example, like the above one, in notepad, and see if you can get it to work. It does work.
Just so you can see for yourself it works :D

And then - when you want to integrate it, make an entirely empty aspx (completely empty, just the @page registry line, no form, no nothing) page, and use response.write in the code behind (or a similar technique) to write a HTML form to the page, and use javascript to submit it.

That's what I did when integrating a payment solution on a website some years back and it worked like a charm. (not paypal, but similar technique as what you try to achieve from what I can see)

Edit: Response.Redirect will not do this, so no need to look further at that one by the way for what you are trying to achieve.

HanneSThEGreaT
July 4th, 2008, 01:34 AM
Mine also worked as Alsvha mentioned, and I also did it in pure HTML :confused:

GremlinSA
July 4th, 2008, 05:11 AM
Well you guys are going to laugh about this one ..

I sat with TB this morning and went over his problem... and we found it .....

he forgot to put a set of "()" in the javascript code..

I'll let him post the final code snip ..

HanneSThEGreaT
July 4th, 2008, 05:20 AM
I'm just happy this is solved :)

Alsvha
July 4th, 2008, 07:12 AM
Well you guys are going to laugh about this one ..

I sat with TB this morning and went over his problem... and we found it .....

he forgot to put a set of "()" in the javascript code..

I'll let him post the final code snip ..

Well - he's not the first one to have done that :o - and he'll not be the last :D