0

I have a page in .Net that currently does some processing of info when a button is clicked. It posts back, updates some info and then redirects the user onwards.

What I want to do now is for this same button, when it's clicked, the info is updated but instead of a redirect it does a POST to another site. The reason being that this other site needs to read a bunch of data from the form I submit.

So, I know about the PostBackUrl property but that will stop the processing of the data that I need done.

So is there another way for me to be able to somehow combine both a postback that then becomes as POST to another site?

Or alternatively some way for me to be able to do the updates I need and then do a POST?

Full Time Skeleton
  • 1,680
  • 1
  • 22
  • 39
  • In the end this was the only solution that worked for me: [enter link description here][1] [1]: http://stackoverflow.com/a/2802848/989348 – Full Time Skeleton Feb 11 '13 at 14:16
  • Marked for some reason as a 'trivial answer'. Odd, seeing as it is a working answer. Maybe I should have copied and pasted it here or waffled on for a few pages so that it wouldn't be considered trivial. – Full Time Skeleton Feb 11 '13 at 14:46

4 Answers4

1

The suggested solutions all kind-of worked but the only one that actually did exactly what I needed it to was this one:

Link to SO answer

The reason the other answers above didn't work is that I was POSTing to a payment gateway and for whatever reason their system thought there was a problem with various missing fields in all solutions except the one I linked to. No idea why, I don't have access to their systems to know what they were actually doing.

In any case, thanks for all answers but have a look at the linked one if you're running into a similar issue.

Community
  • 1
  • 1
Full Time Skeleton
  • 1,680
  • 1
  • 22
  • 39
0

if PostBack isn't absolutely needed, you can actually send them in the request query itself.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

You can do a POST from codebehind, you can find details in this answer Redirect to another page using Post method from Code behind

Community
  • 1
  • 1
Shagglez
  • 1,522
  • 3
  • 21
  • 38
0

If I got your question right I think you will need to get all the form data from Request.Form and make a HttpWebRequest to the other site:

string url = "http://anothersite.com/";

// create post data
StringBuilder postDataBuilder = new StringBuilder();
foreach (var key in this.Request.Form.AllKeys)
{
    postDataBuilder.AppendFormat("{0}={1}&", this.Request.Form[key]);
}

string postData = postDataBuilder.ToString();

// create the web request for the POST
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = postData.Length;

// add the post data
using (StreamWriter requestStream = new StreamWriter(webRequest.GetRequestStream()))
{
    requestStream.Write(postData);
}

Hope this helps!

Vasil Trifonov
  • 1,857
  • 16
  • 21
  • THanks, tried that but the site that I'm posting to is giving an error. Not sure why yet. – Full Time Skeleton Feb 07 '13 at 17:35
  • I think it's a specific error to the payment gateway that I'm trying to post to. Basically it's saying one of the fields is empty or malformed - it isn't. I've sent them the example and they can find nothing wrong with it. I have to presume it's down to use the above method rather than a normal form POST. – Full Time Skeleton Feb 08 '13 at 11:14