Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to redirect client to submit form using POST method?

How to redirect client to submit form using POST method?

Scheduled Pinned Locked Moved ASP.NET
questionjavascriptdatabasecomsysadmin
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    raymonxo
    wrote on last edited by
    #1

    In one of my pages (we'll call this page http://myserver.com/page1.aspx) I need to have a button click event handler do the following two things: 1. Update a database record. 2. Force the client to submit a form using the POST method to a page on another server (we'll call this page http://thirdparty.com/submit.aspx). The key is that step 2 occur using the POST method (not the GET method). Solution using GET method ------------------------------- I am able to do this using the GET method as follows (this code is in the http://myserver.com/page1.aspx code-behind page): [code] // This is pseudocode button_click() { Update database record Construct URL to 3rd party server (i.e. http://thirdparty.com/submit.aspx?a=1&b=2) Response.Redirect(URL) } [/code] This works because the form parameters are embedded in the URL. A Solution using POST method ----------------------------------- The only way I've been able to think of doing this using the POST method is as follows (this code is in the http://myserver.com/page1.aspx code-behind page): button_click() { Update database record Construct URL to another page on my server (we'll call this page http://myserver.com/page2.aspx) Response.Redirect(URL) } The page http://myserver.com/page2.aspx contains a form with hidden inputs containing the values that need to be submitted. The method for the form is POST and the action is the third party URL http://thirdparty.com/submit.aspx. In addition, it contains javascript that will auto-submit the form after the page loads. Question ---------- As you can see, this solution is somewhat complex (I believe it's overly complex). Is there a simple way to force a client to submit a form using the POST method without redirecting the client to a page that contains the form? In other words, is there a programmatic way I can do this without requiring that the client be redirected from my first page to my second page only so the second page can immediately submit? Thanks in advance for any suggestions you have!

    T 1 Reply Last reply
    0
    • R raymonxo

      In one of my pages (we'll call this page http://myserver.com/page1.aspx) I need to have a button click event handler do the following two things: 1. Update a database record. 2. Force the client to submit a form using the POST method to a page on another server (we'll call this page http://thirdparty.com/submit.aspx). The key is that step 2 occur using the POST method (not the GET method). Solution using GET method ------------------------------- I am able to do this using the GET method as follows (this code is in the http://myserver.com/page1.aspx code-behind page): [code] // This is pseudocode button_click() { Update database record Construct URL to 3rd party server (i.e. http://thirdparty.com/submit.aspx?a=1&b=2) Response.Redirect(URL) } [/code] This works because the form parameters are embedded in the URL. A Solution using POST method ----------------------------------- The only way I've been able to think of doing this using the POST method is as follows (this code is in the http://myserver.com/page1.aspx code-behind page): button_click() { Update database record Construct URL to another page on my server (we'll call this page http://myserver.com/page2.aspx) Response.Redirect(URL) } The page http://myserver.com/page2.aspx contains a form with hidden inputs containing the values that need to be submitted. The method for the form is POST and the action is the third party URL http://thirdparty.com/submit.aspx. In addition, it contains javascript that will auto-submit the form after the page loads. Question ---------- As you can see, this solution is somewhat complex (I believe it's overly complex). Is there a simple way to force a client to submit a form using the POST method without redirecting the client to a page that contains the form? In other words, is there a programmatic way I can do this without requiring that the client be redirected from my first page to my second page only so the second page can immediately submit? Thanks in advance for any suggestions you have!

      T Offline
      T Offline
      tojamismis
      wrote on last edited by
      #2

      You should try looking in to the HttpWebRequest class. It encapsulates everything you should need. Here is an example of it from the .NET SDK string postData="firstone="+inputData; ASCIIEncoding encoding=new ASCIIEncoding(); byte[] byte1=encoding.GetBytes(postData); // Set the content type of the data being posted. myHttpWebRequest.ContentType="application/x-www-form-urlencoded"; // Set the content length of the string being posted. myHttpWebRequest.ContentLength=postData.Length; Stream newStream=myHttpWebRequest.GetRequestStream(); newStream.Write(byte1,0,byte1.Length); Console.WriteLine("The value of 'ContentLength' property after sending the data is {0}",myHttpWebRequest.ContentLength); // Close the Stream object. newStream.Close(); In the code you can set the Method using myHttpWebRequest.Method = "POST"; Remember that the POST method sends all inputs on a form in Key/value pairs, if you can autogenerate this set of key/value pairs and write them into the stream then you will eliminate the need for page2.aspx. Tojamismis
      'In the immortal words of Socrates - "I drank what?".'

      T 1 Reply Last reply
      0
      • T tojamismis

        You should try looking in to the HttpWebRequest class. It encapsulates everything you should need. Here is an example of it from the .NET SDK string postData="firstone="+inputData; ASCIIEncoding encoding=new ASCIIEncoding(); byte[] byte1=encoding.GetBytes(postData); // Set the content type of the data being posted. myHttpWebRequest.ContentType="application/x-www-form-urlencoded"; // Set the content length of the string being posted. myHttpWebRequest.ContentLength=postData.Length; Stream newStream=myHttpWebRequest.GetRequestStream(); newStream.Write(byte1,0,byte1.Length); Console.WriteLine("The value of 'ContentLength' property after sending the data is {0}",myHttpWebRequest.ContentLength); // Close the Stream object. newStream.Close(); In the code you can set the Method using myHttpWebRequest.Method = "POST"; Remember that the POST method sends all inputs on a form in Key/value pairs, if you can autogenerate this set of key/value pairs and write them into the stream then you will eliminate the need for page2.aspx. Tojamismis
        'In the immortal words of Socrates - "I drank what?".'

        T Offline
        T Offline
        tojamismis
        wrote on last edited by
        #3

        Even better, check out this example http://www.netomatix.com/HttpPostData.aspx[^] Tojamismis
        'In the immortal words of Socrates - "I drank what?".'

        A 1 Reply Last reply
        0
        • T tojamismis

          Even better, check out this example http://www.netomatix.com/HttpPostData.aspx[^] Tojamismis
          'In the immortal words of Socrates - "I drank what?".'

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          Thanks you very much!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups