【问题标题】:How can I do a redirect with post variables如何使用帖子变量进行重定向
【发布时间】:2011-04-20 08:44:57
【问题描述】:

我必须进行重定向并将变量ap 的值发送到另一个页面。我不能使用 GET 方法,例如:http://urlpage?a=1&p=2。我必须用 post 方法发送它们。如何在不使用 c# 的表单的情况下发送它们?

【问题讨论】:

标签: c# post redirect


【解决方案1】:

这个类包装了表单。有点hacky,但它有效。只需将 post 值添加到类并调用 post 方法即可。

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

【讨论】:

  • 这很好,但您应该HttpUtility.HtmlEncode 在构建 HTML 时转义您的表单参数。
【解决方案2】:

【讨论】:

    【解决方案3】:

    使用WebClient.UploadStringWebClient.UploadData,您可以轻松地将数据发布到服务器。我将展示一个使用 UploadData 的示例,因为 UploadString 的使用方式与 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);
    

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

    【讨论】:

    • 你必须检查你的答案
    【解决方案4】:

    以下代码适用于 Billdesk PG 集成。非常感谢。

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

    【讨论】:

      【解决方案5】:

      此链接向您说明如何执行以下操作? 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]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-14
        • 1970-01-01
        • 2016-05-30
        • 1970-01-01
        • 1970-01-01
        • 2017-06-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多