用Post数据重定向到URL

本文关键字:URL 重定向 数据 Post | 更新日期: 2023-09-27 18:06:27

我正在为支付提供商的帖子重定向工作,并试图将表单数据传递到他们的安全URL。

我在kentico 8中使用他们的自定义支付网关方法,如下所示https://docs.kentico.com/display/K8/Creating+a+custom+payment+gateway

因此,在自定义支付类中,我准备了将以'Form'格式传递给支付提供商的数据,并使用本教程http://www.codeproject.com/Articles/37539/Redirect-and-POST-in-ASP-NET

隐藏字段

但是我不能弄清楚如何重定向到安全的URL与表单数据?

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

我已尝试响应。重定向,但它是一个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
{

/// <summary>
/// Process payment.
/// </summary>
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("<form id='"" + formID + "'" name='"" + 
                    formID + "'" action='"" + url + 
                    "'" method='"POST'">");
    foreach (string key in data)
    {
        strForm.Append("<input type='"hidden'" name='"" + key + 
                        "'" value='"" + data[key] + "'">");
    }
    strForm.Append("</form>");
    //Build the JavaScript which will do the Posting operation.
    StringBuilder strScript = new StringBuilder();
    strScript.Append("<script language='"javascript'">");
    strScript.Append("var v" + formID + " = document." + 
                        formID + ";");
    strScript.Append("v" + formID + ".submit();");
    strScript.Append("</script>");
    //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();
}

}

用Post数据重定向到URL

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