Kevin Donde
Kevin Donde

Reputation: 922

Add post values and redirect c#

I need to post to another url / server, however I need to add in other values before it goes to the other url / server. So I would like to post back to my own server, add in the values to the "post" then redirect to the new url.

I've done some searching and there are several answers about doing similar on stackoverflow. Including one particularly good one that I can't find anymore that gave 3 or 4 different examples. Post Method and Redirect With Some Values Without Form C# | Post Data To Url and Redirect | Post form data using HttpWebRequest

However they all seem to create a new set of values to post rather than retrieving the current set of post values and then adding to them. One of them indicated that I couldn't post using Response.Redirect(). So specifically ...

  1. How can I retrieve the current "post" and add my own values to it.
  2. How can I then send the user to another url with that posted data? I want the user to go to a different page after adding my own values on the server.

There's a chance this is a duplicate (with the magnitude of questions on stack it's quite possible) ... if someone can find the answers somewhere else please let me know. I'm working on code currently and will add that to the question the minute I have it ... later tonight.

Edit #1

    public ActionResult Index(FormCollection form)
    {
        //string[] outGoingPostValues = new string[Request.Form.AllKeys.Length + 2];

        string[] incomingPostValues = Request.Form.AllKeys;
        foreach (string t in incomingPostValues)
        {
            Response.Write(t + ": " + Request.Form[t] + "<br>");
        }

        // add in two new keys
        Response.Write("Value1: " + "value1" + "<br>");
        Response.Write("Value2: " + "value2" + "<br>");

        var url = "http://newURL.com";
        WebRequest request = WebRequest.Create(url);
        request.Method = "POST";

        return Redirect("http://someurl.com");
  }

I'm currently trying to test it out by redirecting to my own view and trying to print out the following ...

@{
string[] outGoingPostValues = new string[Request.Form.AllKeys.Length + 2];

string[] incomingPostValues = Response. ?????
foreach (string key in incomingPostValues)
{
    Html.Raw(key);
}  
}

Upvotes: 2

Views: 4496

Answers (2)

Vadim K.
Vadim K.

Reputation: 1101

I assume you don't have control over the app you are redirecting to. In this case I don't see why approach from this question wouldn't work for you? Unless the values you are adding are sensitive info.

You can do the following:

  1. Get the post data from user.
  2. Massage it as you need, add any new values that you need.
  3. Render a form with massaged values into client response.
  4. Add javascript snippet that would post the form to the other page on load.

Sample code:

 Dictionary<string, string> postData = new Dictionary<string, string>();
 foreach (string key in Request.Form.AllKeys)
 {
        postData.Add(key, Request.Form[key]);
 }
 postData.Add("new-key", "new-value"); //add new values here.
 ViewBag.formPostData = postData;

In a view:

@using (@Html.BeginForm(null, null, FormMethod.Post, new { @action = "http://redirect.com/", @id = "redirectForm" }))
{
    foreach (KeyValuePair<string, string> item in @ViewBag.formPostData)
    {
        @Html.Hidden(item.Key, item.Value);
    }
}


<script type="text/javascript">
    $(document).ready(function () {
        var form = $('#redirectForm');
        if (form) {
            form.submit();
        }
    })
</script>

EDIT:

If the new values are sensitive, I'm afraid there wouldn't be an easy way to accomplish what you want w/o having control over another app. You can execute HttpRequest on the server side with appropriate post parameters as mentioned in other answers and then render the response to the user, but they would still see YOUR url at the top. And resources from the other site such as images, css, etc. might not work properly. And you need to understand that your scenario creates a lot of security concerns.

Upvotes: 2

Imran Ali
Imran Ali

Reputation: 104

You probably need to do something like How to Get the HTTP Post data in C#? to get the data from the post.

Then edit the data however you wish to. Then send the data to the new website using a HttpResponse or Webclient with the POST condition (if there is authentication involved you should look into saving the cookies and sending those over too). Your sources seem to show you how to do that.

Looking at your edit, it seems you forgot to do some things so here is some sample code:

        WebRequest request = WebRequest.Create ("http://www.contoso.com/PostAccepter.aspx ");

        // Set the Method property of the request to POST.
        request.Method = "POST";

        // Create POST data and convert it to a byte array.
        string postData = "This is a test that posts this string to a Web server.";

        byte[] byteArray = Encoding.UTF8.GetBytes (postData);

        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";

        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;
        // Get the request stream.
        Stream dataStream = request.GetRequestStream ();

        // Write the data to the request stream.
        dataStream.Write (byteArray, 0, byteArray.Length);

        // Close the Stream object.
        dataStream.Close ();

Taken from How to: Send Data Using the WebRequest Class

Upvotes: 1

Related Questions