Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Duplicating a Simple HTML Form Submission (POST)This example shows how to duplicate a simple HTML form submission. The HTML to be duplicated is shown below, and may be tested in a browser by going to this URL: http://www.chilkatsoft.com/postSample.html <html> <body> <form action="echoPost.asp" method="post"> <p>First name: <input name="firstname"></p> <p>Last name: <input name="lastname"></p> <p>Your favorite color: <select name="color"> <option>Blue</option> <option>Green</option> <option>Red</option> <option>Yellow</option> <option>Pink</option> </select> </p> <input type="hidden" name="myHiddenField1", value="Hidden Value 1"> <input type="hidden" name="myHiddenField2", value="Hidden Value 2"> <p><input type="submit" value="Send POST"></p> </form> </body> </html> By default, a browser will send a form POST as an HTTP reqeuest using a Content-Type of "application/x-www-form-urlencoded". This is because the form parameters are sent in the body of the HTTP POST using the URL encoding. Therefore, one should use the PostUrlEncoded method to duplicate the form submission. Each "input" or "select" tag found in the HTML is a form parameter having a name and value. These are duplicated in code by calling AddParam on the HTTP Request object for each parameter. The target URL is indicated by the "action" attribute on the "form" tag. In the HTML form shown above, it is "echoPost.asp". The URL specified in the "action" attribute may be an absolute or relative URL. In this case, it is a relative URL, and since there is no directory path, the full URL of the POST target is "http://www.chilkatsoft.com/echoPost.asp". This will be the 1st argument passed to the PostUrlEncoded method. The raw HTTP POST sent to the web server in this example is shown below. The form parameters are located in the body of the request (i.e. under the header fields), and they are URL encoded. POST /echoPost.asp HTTP/1.1 Content-Type: application/x-www-form-urlencoded Host: www.chilkatsoft.com Content-Length: 87 firstname=John&lastname=Doe&myHiddenField1=Hidden+Value+1&myHiddenField2=Hidden+Value+2 Note: An HTTP file upload is a form submission having an "input" tag w/ "type=file". In this case, a browser will send the POST using a content-type of "multipart/form-data". The PostUrlEncoded method should not be called for HTTP file uploads. The SynchronousRequest would instead be called.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @iTmp0 int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) -- This example requires the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @http int -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @req int -- Use "Chilkat_9_5_0.HttpRequest" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT -- Add the form parameters to the HTTP request: EXEC sp_OAMethod @req, 'AddParam', NULL, 'firstname', 'John' EXEC sp_OAMethod @req, 'AddParam', NULL, 'lastname', 'Doe' EXEC sp_OAMethod @req, 'AddParam', NULL, 'myHiddenField1', 'Hidden Value 1' EXEC sp_OAMethod @req, 'AddParam', NULL, 'myHiddenField2', 'Hidden Value 2' -- Send the HTTP POST by calling PostUrlEncoded. -- The Content-Type used w/ the request will be application/x-www-form-urlencoded DECLARE @resp int EXEC sp_OAMethod @http, 'PostUrlEncoded', @resp OUT, 'http://www.chilkatsoft.com/echoPost.asp', @req EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 = 0 BEGIN EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @http EXEC @hr = sp_OADestroy @req RETURN END -- Display the Body of the HTTP resp -- (This is the HTML that the browser would receive if it -- had been an interactive form submission from the browser.) EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @resp EXEC @hr = sp_OADestroy @http EXEC @hr = sp_OADestroy @req END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.