使用post数据发布重定向到url

我正在为付款提供商进行邮寄重定向,并尝试将表单数据传递到其安全URL。

我使用他们的自定义支付网关方法在ken​​tico 8中执行此操作,如此处所示https://docs.kentico.com/display/K8/Creating+a+custom+payment+gateway

因此,在自定义支付类中,我准备了使用本教程http://www.codeproject.com/Articles/37539/Redirect-and-POST-in以隐藏字段的“表单”格式传递给支付提供商的数据。 -Asp-NET

但是,我无法弄清楚如何使用表单数据重定向到安全URL?

到目前为止,这是我的代码。

我尝试过Response.Redirect,但它是一个GET函数而不是POST。

using System; using System.Collections; using System.Collections.Specialized; using System.Configuration; using CMS; using CMS.Base; using CMS.EcommerceProvider; using CMS.Helpers; using System.Security.Cryptography; using System.Web; using System.Web.Security; using System.Text; using System.IO; using System.Net; using System.Web.UI; [assembly: RegisterCustomClass("CustomGateway", typeof(CustomGateway))] public class CustomGateway : CMSPaymentGatewayProvider { ///  /// Process payment. ///  public override void ProcessPayment() { // Get payment gateway url string url = "https://epage.payandshop.com/epage.cgi"; if (url != "") { NameValueCollection postData = getRealexData(); RedirectAndPOST(url, postData); } else { // Show error message - payment gateway url not found ErrorMessage = "Unable to finish payment: Payment gateway url not found."; // Update payment result PaymentResult.PaymentDescription = ErrorMessage; PaymentResult.PaymentIsCompleted = false; // Update order payment result in database UpdateOrderPaymentResult(); } } public static void RedirectAndPOST(string destinationUrl, NameValueCollection data) { //Prepare the Posting form string strForm = PreparePOSTForm(destinationUrl, data); HttpContext.Current.Response.Write(strForm); HttpContext.Current.Response.End(); } private static String PreparePOSTForm(string url, NameValueCollection data) { //Set a name for the form string formID = "PostForm"; //Build the form using the specified data to be posted. StringBuilder strForm = new StringBuilder(); strForm.Append("
"); foreach (string key in data) { strForm.Append(""); } strForm.Append("
"); //Build the JavaScript which will do the Posting operation. StringBuilder strScript = new StringBuilder(); strScript.Append(""); strScript.Append("var v" + formID + " = document." + formID + ";"); strScript.Append("v" + formID + ".submit();"); strScript.Append(""); //Return the form and the script concatenated. //(The order is important, Form then JavaScript) return strForm.ToString() + strScript.ToString(); } public NameValueCollection getRealexData() { //format the date expected by Realex string timestamp = RealexDateFormatter.DateFormatForRealex(); //take the MerchantID and Shared Secret from the web.config string merchantid = ConfigurationManager.AppSettings["RealexMerchantID"]; string secret = ConfigurationManager.AppSettings["RealexSecret"]; // Order Info int orderid = ShoppingCartInfoObj.OrderId; string stringorderid = Convert.ToString(orderid); string curr = ShoppingCartInfoObj.Currency.CurrencyCode; double amount = ShoppingCartInfoObj.TotalPrice; amount = Convert.ToInt32(amount); string prepareMD5 = timestamp + "." + merchantid + "." + orderid + "." + amount + "." + curr; //generate the md5 Hash MD5 md5 = new MD5CryptoServiceProvider(); string temp1 = GetMD5Hash(prepareMD5); temp1 = temp1.ToLower(); string temp2 = temp1 + "." + secret; string md5hash = GetMD5Hash(temp2); md5hash = md5hash.ToLower(); NameValueCollection data = new NameValueCollection(); data.Add("MERCHANT_ID", merchantid); data.Add("ORDER_ID", stringorderid); data.Add("CURRENCY", curr); data.Add("AMOUNT", amount.ToString()); data.Add("TIMESTAMP", timestamp); data.Add("MD5HASH", md5hash); data.Add("AUTO_SETTLE_FLAG", "1"); return data; } public static String GetMD5Hash(String TextToHash) { //Check wether data was passed if ((TextToHash == null) || (TextToHash.Length == 0)) { return String.Empty; } //Calculate MD5 hash MD5 md5 = new MD5CryptoServiceProvider(); // Create a new Stringbuilder to collect the bytes // and create a string. byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(TextToHash)); StringBuilder sBuilder = new StringBuilder(); // Loop through each byte of the hashed data // and format each one as a hexadecimal string. for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } return sBuilder.ToString(); }

}

从类似的问题看一下这个答案 。 我建议在答案中使用方法3。 通过创建异步调用,您可以等待响应并使用Response.Redirect()重定向成功响应,或者通知用户任何错误。