26

I want to implement a Payment service.I will create some values in code behind and then by using post method I have to post this values to Payment gateway and user must redirect to that page.

I can't Use form action becuase I have to create some values and save some thing in db in code behind.

how can I implement this? If I can post data to another page on my app and can submit that page programmically it maybe help me.

Thanks

Ashian
  • 521
  • 2
  • 7
  • 22

1 Answers1

20
string url = "3rd Party Url";

StringBuilder postData = new StringBuilder();

postData.Append("first_name=" + HttpUtility.UrlEncode(txtFirstName.Text) + "&");
postData.Append("last_name=" + HttpUtility.UrlEncode(txtLastName.Text));

//ETC for all Form Elements

// Now to Send Data.
StreamWriter writer = null;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";                        
request.ContentLength = postData.ToString().Length;
try
{
    writer = new StreamWriter(request.GetRequestStream());
    writer.Write(postData.ToString());
}
finally
{
    if (writer != null)
        writer.Close();
}

Response.Redirect("NewPage");

Have a look at this Poster

Community
  • 1
  • 1
Asad
  • 21,468
  • 17
  • 69
  • 94
  • i am implementing cashu regarding the same but during response.write it shows the following error: The underlying connection was closed: The connection was closed unexpectedly. also i need to redirect it to the cashu page – sfdc-neta Apr 02 '14 at 09:34
  • 11
    what is "New Page" in this?? – Jahangeer Jun 13 '14 at 16:10
  • 1
    So this solution works going to “NewPage” with parameters. I’m also trying to set session cookie (so logging the user in) in target website but the cookie doesn’t seem to set for some reason. Eg. Site A POST to Site B. Inside Site B I’m trying to set FormsAuthentication cookie. Any ideas? – DrZeuso Apr 03 '19 at 05:20