我的情况是这样的,我必须将用户重定向到paypal网站以及数据,因为当Paypal付款页面显示时,用户信息将自动填满。我试过但失败了。本地我尝试模拟这里的问题是代码。
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}¶meter2={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页面中提取这些数据,并在下面显示代码
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Response.Write( Request.Form["parameter1"]);
Response.Write(Request.Form["parameter2"]);
}
}
所以请指导我,如果paypal网站将在浏览器中打开,我将如何以编程方式将用户重定向到paypal网站以及数据,然后客户详细信息将填写在那里。再一次提到我需要从代码背后以编程方式完成所有事情。感谢
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));
}
}
答案 0 :(得分: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')
等