Reputation: 34198
my situation is something like that i have to redirect user to paypal site along with data as a result when paypal payment page will display then user information will filled up automatically. i tried but failed. locally i try to simulate the issue here is the code.
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");
the above code redirect user to test1.aspx page along with data
and i try to extract those data from test1.aspx page and display there like below code
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Response.Write( Request.Form["parameter1"]);
Response.Write(Request.Form["parameter2"]);
}
}
so please guide me that how can i redirect user programmatically to paypal site along with data as a result when paypal site will open in browser then customer detail will be filled up there. mention once again i need to do the whole thing programmatically from code behind. thanks
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));
}
}
Upvotes: 1
Views: 4000
Reputation: 2344
this was some code i've used several times in the past to do remote posts using c#.net,
I am unsure as to who to accredit the code to, as it's not mine.
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();
}
}
then you'd call it as simply
RemotePost myPost = new RemotePost('www.paypal.com')
etc
Upvotes: 4