如何使用post变量进行重定向

时间:2011-04-20 08:44:58

标签: c# post redirect

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

5 个答案:

答案 0 :(得分:5)

此类包装表单。有点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();
    }
}

答案 1 :(得分:1)

答案 2 :(得分:0)

使用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/

答案 3 :(得分:0)

以下代码对我来说适用于Billdesk PG Integration。非常感谢。

source: LocalDataSource = new LocalDataSource();
pageSize = 25;

ngOnInit() {
  this.source.onChanged().subscribe((change) => {
    if (change.action === 'page') {
      this.pageChange(change.paging.page);
    }
  });
}

pageChange(pageIndex) {
  const loadedRecordCount = this.source.count();
  const lastRequestedRecordIndex = pageIndex * this.pageSize;

  if (loadedRecordCount <= lastRequestedRecordIndex) {    
    let myFilter; //This is your filter.
    myFilter.startIndex = loadedRecordCount + 1;
    myFilter.recordCount = this.pageSize + 100; //extra 100 records improves UX.

    this.myService.getData(myFilter) //.toPromise()
      .then(data => {
        if (this.source.count() > 0)
          data.forEach(d => this.source.add(d));
        else
          this.source.load(data);
      })
  }
}

答案 4 :(得分:-1)

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