Redirect to another page using Post method from Code behind
Asked Answered
S

1

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

Sihonn answered 13/2, 2010 at 20:9 Comment(0)
C
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

Coax answered 13/2, 2010 at 20:26 Comment(3)
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 pageIntrauterine
what is "New Page" in this??Hydrops
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?Gunyah

© 2022 - 2024 — McMap. All rights reserved.