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?

2
  • In the end this was the only solution that worked for me: [enter link description here][1] [1]: stackoverflow.com/a/2802848/989348 Commented Feb 11, 2013 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. Commented Feb 11, 2013 at 14:46

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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

Comments

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!

2 Comments

THanks, tried that but the site that I'm posting to is giving an error. Not sure why yet.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.