title: Redirect user to Paypal site along with data programmatically from codebehind
body: <p>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.</p>
  
  <h2>test.aspx</h2>
  
  <pre><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}&amp;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");
  </code></pre>
  
  <p>the above code redirect user to test1.aspx page along with data</p>
  
  <p>and i try to extract those data from test1.aspx page and display there like below code</p>
  
  <h2>test1.aspx</h2>
  
  <pre><code> protected void Page_Load(object sender, EventArgs e)
  {
      if (IsPostBack)
      {
          Response.Write( Request.Form["parameter1"]);
          Response.Write(Request.Form["parameter2"]);
      }
  }
  </code></pre>
  
  <p>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</p>
  
  <h2>This solution i got and it is better</h2>
  
  <pre><code>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
  {
  /// &lt;summary&gt;
  /// This method prepares an Html form which holds all data in hidden field in the addetion to form submitting script.
  /// &lt;/summary&gt;
  /// &lt;param name="url"&gt;The destination Url to which the post and redirection will occur, the Url can be in the same App or ouside the App.&lt;/param&gt;
  /// &lt;param name="data"&gt;A collection of data that will be posted to the destination Url.&lt;/param&gt;
  /// &lt;returns&gt;Returns a string representation of the Posting form.&lt;/returns&gt;
  /// &lt;Author&gt;Samer Abu Rabie&lt;/Author&gt;
  
  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("&lt;form id=\"" + formID + "\" name=\"" + formID + "\" action=\"" + url + "\" method=\"POST\"&gt;");
      foreach (string key in data)
      {
          strForm.Append("&lt;input type=\"hidden\" name=\"" + key + "\" value=\"" + data[key] + "\"&gt;");
      }
      strForm.Append("&lt;/form&gt;");
  
      //Build the JavaScript which will do the Posting operation.
      StringBuilder strScript = new StringBuilder();
      strScript.Append("&lt;script language='javascript'&gt;");
      strScript.Append("var v" + formID + " = document." + formID + ";");
      strScript.Append("v" + formID + ".submit();");
      strScript.Append("&lt;/script&gt;");
  
      //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));
  }
  
  }
  </code></pre>