1

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

测试.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 页面中提取这些数据并在那里显示如下代码

测试1.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));
}

}
4

1 回答 1

4

这是我过去多次使用 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')ETC

于 2012-06-01T10:16:09.413 回答