开发者

How can I do a redirect with post variables

I have to do a redirect and send to another page the value of variables a a开发者_JAVA百科nd 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#?


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();
    }
}


this is your answer :

Redirect to another page using Post method from Code behind


Using WebClient.UploadString or WebClient.UploadData you can POST data to the server easily. I’ll show an example using UploadData, since UploadString is used in the same manner as DownloadString.

byte[] bret = client.UploadData("http://www.website.com/post.php", "POST",
          System.Text.Encoding.ASCII.GetBytes("field1=value1&amp;field2=value2") );

string sret = System.Text.Encoding.ASCII.GetString(bret);

more : http://www.daveamenta.com/2008-05/c-webclient-usage/


The below code worked for me for Billdesk PG Integration. Thanks a lot.

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();
    }
}


This link explain you how to do the following? http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

using System.Net;
...
string HttpPost (string uri, string parameters)
{ 
   // parameters: name1=value1&name2=value2 
   WebRequest webRequest = WebRequest.Create (uri);
   //string ProxyString = 
   //   System.Configuration.ConfigurationManager.AppSettings
   //   [GetConfigKey("proxy")];
   //webRequest.Proxy = new WebProxy (ProxyString, true);
   //Commenting out above required change to App.Config
   webRequest.ContentType = "application/x-www-form-urlencoded";
   webRequest.Method = "POST";
   byte[] bytes = Encoding.ASCII.GetBytes (parameters);
   Stream os = null;
   try
   { // send the Post
      webRequest.ContentLength = bytes.Length;   //Count bytes to send
      os = webRequest.GetRequestStream();
      os.Write (bytes, 0, bytes.Length);         //Send it
   }
   finally
   {
      if (os != null)
      {
         os.Close();
      }
   }

   try
   { // get the response
      WebResponse webResponse = webRequest.GetResponse();
      if (webResponse == null) 
         { return null; }
      StreamReader sr = new StreamReader (webResponse.GetResponseStream());
      return sr.ReadToEnd ().Trim ();
   }
   return null;
} // end HttpPost 
[edit]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜