,

Redirect user to url with POST method from code behind asp.net

Posted by

Previously I have issue when I did integration with payment gateway that require user to submit data to the payment gateway url with POST method.
The issue is I need to process the data from user input first, then I need to redirect user to payment gateway with form data. When received input from user in code behind, I need to process the user input and then this data then will be post to the payment gateway and user will be redirected to the payment gateway. You can do HttpWebRequest if it is only happen at the server level. BUT in this scenario user must be redirected to the payment gateway url with the processed data. How to do that in asp.net.? Here is the solution I use to overcome the issue. 

RemotePostHTMLForm Class

First I write one class called RemotePostHTMLForm.

public class RemotePostHTMLForm
{
    private System.Collections.Specialized.NameValueCollection Inputs
    = new System.Collections.Specialized.NameValueCollection();

    public string Url = "";
    public string Method = "post";
    public string FormName = "form1";

    public void Add(string name, string value)
    {
        Inputs.Add(name, value);
    }
    public void Post()
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Write("<html><head>");
        HttpContext.Current.Response.Write(string.Format("</head><body onload=\"document.{0}.submit()\">", FormName));
        HttpContext.Current.Response.Write(string.Format("<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >", FormName, Method, Url));
        for (int i = 0; i < Inputs.Keys.Count; i++)
        {
            HttpContext.Current.Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", Inputs.Keys[i], Inputs[Inputs.Keys[i]]));
        }
        HttpContext.Current.Response.Write("</form>");
        HttpContext.Current.Response.Write("</body></html>");
        HttpContext.Current.Response.End();
    }
} 

 

How to use it.

  • First you add the processed user input into dictionary.
  • Init class RemotePostHtmlForm.
  • Iterate your collection data in dictionary and use Add method in RemotePostHtmlForm class.
  • Call post() method.
  • post() method will write html code at client side with hidden input in your dictionary and post it to payment gateway.User will get redirected to payment gateway with data form data post to payment gateway.
        IDictionary<string, object> paymentDatas = new Dictionary<string, object>();
        paymentDatas.Add("Amount", "1.00");
        paymentDatas.Add("Currency", "MYR");
        paymentDatas.Add("Checksum", "4dc5e4d81f795dfbcabb87b5821aae27e41887dea2388eeee8bb7b612a9bf0c6ded6761bbe71a083b5f394dfbd4cd5ff92106b7c134ec54dd8928342c6fb881a");
        RemotePostHTMLForm myremotepost = new RemotePostHTMLForm();
        myremotepost.Url = gatewayUrl;
        foreach (string key in paymentData.Keys)
        {
            myremotepost.Add(key, ValidationHelper.GetString(paymentData[key], ""));
        }
        myremotepost.Post();