Friday 21 December 2012

Post Data Using Response.Redirect In Asp.net


Hello Every one after long time I am back with new Post that is you Post Data Using Response.Redirect in your application. Nowdays I am creating a application on which I needed to communicate with a third party payment application, where in some point, I will have to send information to that third party application using POST.
we are not using GET method because it contain secure value.

In Our asp.net there some options for submitting data(POST or GET) to a destination URL: They Are As follow:

Response.Redirect: This method is commonly used, but it will only use the GET method in ASP.NET, and there is no way to do a POST using it.

Server.Transfer: This method does a POST not GET, but ... unfortunately, it will only work when the source and destination are in the same application;

HTML Submit button with POST FORM: Well, this is the standard way of sending POST data to any URL, whether it is in your application or in a third party application, but this will restrict you to have a Submit button.

Now I have a Response.Redirect method to which give a collection of data and to do a POST method instead of a GET method


For that you have to follow following Steps: STEP 1 : CREATE CLASS WITH NAME PostReponceRedirect
this class Contain two method
1)PostNRedirect()
2)createPostForm()
Now, The PostNRedirect Method Contain Following Code they Are :
public static void PostNRedirect(Page page, string destinationUrl, 
                                   NameValueCollection value)
{
// call to create the Posting form
string FormContain = createPostForm(destinationUrl, value);

//the Post Form, this is to submit the Posting form with the request.
page.Controls.Add(new LiteralControl(FormContain ));
}



Noe 2nd one that is createPostForm contain following code they Are :
private static String createPostForm(string url, NameValueCollection value)
{
    //Set a name for the form
    string formID = "PostForm";

    //Build the form using the specified data to be posted.
    StringBuilder FormContain = new StringBuilder();
    FormContain .Append("
"); foreach (string key in value) { FormContain .Append(""); } FormContain .Append("
"); //Build the JavaScript which will do the Posting operation. StringBuilder jScript = new StringBuilder(); jScript .Append(""); //Return the form and the script concatenated. //(The order is important, Form then JavaScript) return FormContain .ToString() + jScript .ToString(); }

Now the Finally call that class methos in your page using following Code they are :
NameValueCollection value= new NameValueCollection();
value.Add("key1", "val1");
value.Add("key2", "val2");
PostReponceRedirect.PostNRedirect(this.Page, "your page url", value);

I hope it will help you in your coding "HAPPY CODING"
Thank You So Much for reading my Post
Chetan Virkar

No comments:

Post a Comment