Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do a redirect with post variables

Tags:

redirect

c#

post

I have to do a redirect and send to another page the value of variables a and p. I can't use the GET method like: http://urlpage?a=1&p=2. I have to send them with the post method. How can I send them without use a form from c#?

like image 215
Luca Romagnoli Avatar asked Apr 20 '11 08:04

Luca Romagnoli


1 Answers

This class wraps the form. Kind of hacky but it works. Just add the post values to the class and call the post method.

  public class RemotePost
{
    private Dictionary<string, string> Inputs = new Dictionary<string, string>();
    public string Url = "";
    public string Method = "post";
    public string FormName = "form1";
    public StringBuilder strPostString;

    public void Add(string name, string value)
    {
        Inputs.Add(name, value);
    }
    public void generatePostString()
    {
        strPostString = new StringBuilder();

        strPostString.Append("<html><head>");
        strPostString.Append("</head><body onload=\"document.form1.submit();\">");
        strPostString.Append("<form name=\"form1\" method=\"post\" action=\"" + Url + "\" >");

        foreach (KeyValuePair<string, string> oPar in Inputs)
            strPostString.Append(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", oPar.Key, oPar.Value));

        strPostString.Append("</form>");
        strPostString.Append("</body></html>");
    }
    public void Post()
    {
        System.Web.HttpContext.Current.Response.Clear();
        System.Web.HttpContext.Current.Response.Write(strPostString.ToString());
        System.Web.HttpContext.Current.Response.End();
    }
}
like image 147
Jaime Enrique Espinosa Reyes Avatar answered Oct 14 '22 04:10

Jaime Enrique Espinosa Reyes