以编程方式将用户重定向到 Paypal 站点以及来自代码隐藏的数据

Posted

技术标签:

【中文标题】以编程方式将用户重定向到 Paypal 站点以及来自代码隐藏的数据【英文标题】:Redirect user to Paypal site along with data programmatically from codebehind 【发布时间】:2012-06-06 13:36:19 【问题描述】:

我的情况类似于我必须将用户重定向到贝宝网站以及数据,结果当贝宝支付页面显示时,用户信息将自动填充。我试过但失败了。在本地我尝试模拟这里的问题是代码。

test.aspx

WebRequest request = WebRequest.Create("http://localhost:14803/PaypalCSharp/Test1.aspx");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string postContent = string.Format("parameter1=0&parameter2=1", "Hello", "Wow");
byte[] postContentBytes = Encoding.ASCII.GetBytes(postContent);
request.ContentLength = postContentBytes.Length;
Stream writer = request.GetRequestStream();
writer.Write(postContentBytes, 0, postContentBytes.Length);
writer.Close();
Response.Redirect("test1.aspx");

以上代码将用户连同数据一起重定向到 test1.aspx 页面

我尝试从 test1.aspx 页面中提取这些数据并像下面的代码一样显示在那里

test1.aspx

 protected void Page_Load(object sender, EventArgs e)

    if (IsPostBack)
    
        Response.Write( Request.Form["parameter1"]);
        Response.Write(Request.Form["parameter2"]);
    

所以请指导我如何以编程方式将用户重定向到贝宝网站以及数据,因为当贝宝网站将在浏览器中打开时,客户详细信息将在那里填写。再次提到我需要从后面的代码以编程方式完成整个事情。谢谢

我得到了这个解决方案,它更好

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.htmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Collections.Specialized;
using System.Text;

public static class HttpHelper

/// <summary>
/// This method prepares an Html form which holds all data in hidden field in the addetion to form submitting script.
/// </summary>
/// <param name="url">The destination Url to which the post and redirection will occur, the Url can be in the same App or ouside the App.</param>
/// <param name="data">A collection of data that will be posted to the destination Url.</param>
/// <returns>Returns a string representation of the Posting form.</returns>
/// <Author>Samer Abu Rabie</Author>

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 static void RedirectAndPOST(Page page, string destinationUrl, NameValueCollection data)

    //Prepare the Posting form
    string strForm = PreparePOSTForm(destinationUrl, data);

    //Add a literal control the specified page holding the Post Form, this is to submit the Posting form with the request.
    page.Controls.Add(new LiteralControl(strForm));



【问题讨论】:

这有帮助吗? codersbarn.com/post/2008/03/08/… issue regarding post data programmatically to another aspx page的可能重复 【参考方案1】:

这是我过去多次使用 c#.net 进行远程发帖的一些代码,

我不确定将代码授权给谁,因为它不是我的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Specialized;
using System.Net;


/// <summary>
/// Summary description for RemotePost
/// </summary>
public partial class RemotePost

    private NameValueCollection inputValues;

    /// <summary>
    /// Gets or sets a remote URL
    /// </summary>
    public string Url  get; set; 

    /// <summary>
    /// Gets or sets a method
    /// </summary>
    public string Method  get; set; 

    /// <summary>
    /// Gets or sets a form name
    /// </summary>
    public string FormName  get; set; 

    public NameValueCollection Params
    
        get
        
            return inputValues;
        
    

    /// <summary>
    /// Creates a new instance of the RemotePost class
    /// </summary>
    public RemotePost()
    
        inputValues = new NameValueCollection();
        Url = "http://www.someurl.com";
        Method = "post";
        FormName = "formName";
    

    /// <summary>
    /// Adds the specified key and value to the dictionary (to be posted).
    /// </summary>
    /// <param name="name">The key of the element to add</param>
    /// <param name="value">The value of the element to add.</param>
    public void Add(string name, string value)
    
        inputValues.Add(name, value);
    


    /// <summary>
    /// Post
    /// </summary>
    public void Post()
    
        var context = HttpContext.Current;
        context.Response.Clear();
        context.Response.Write("<html><head>");
        context.Response.Write(string.Format("</head><body onload=\"document.0.submit()\">", FormName));
        context.Response.Write(string.Format("<form name=\"0\" method=\"1\" action=\"2\" >", FormName, Method, Url));
        for (int i = 0; i < inputValues.Keys.Count; i++)
            context.Response.Write(string.Format("<input name=\"0\" type=\"hidden\" value=\"1\">", HttpUtility.HtmlEncode(inputValues.Keys[i]), HttpUtility.HtmlEncode(inputValues[inputValues.Keys[i]])));
        context.Response.Write("</form>");
        context.Response.Write("</body></html>");
        context.Response.End();
    


那你就这么简单地称呼它

RemotePost myPost = new RemotePost('www.paypal.com')

【讨论】:

感谢您的帮助。你的代码有效。我有更好的代码。看看我更新的问题。谢谢 @Thomas(我意识到这已经很晚了)当有人回答你的第一个问题时,最好介绍一个新帖子来解决未来的问题。 你应该使用 HttpUtility.HtmlAttributeEncode 而不是 HttpUtility.HtmlEncode

以上是关于以编程方式将用户重定向到 Paypal 站点以及来自代码隐藏的数据的主要内容,如果未能解决你的问题,请参考以下文章

ReactJS:如何以编程方式将用户重定向到另一个页面?

使用带有 Flux 的 React Router 以编程方式重定向

如何以稳健的方式实现贝宝代码

将数据提交到 db 后将用户重定向到 PayPal

PHP POST 到 URL,用户被重定向

PayPal付款方式无法正常工作