Sample code for 30+ languages & platforms
SQL Server

HTTP Form Authentication

See more HTTP Examples

The authentication scheme used by any given web site can vary based on its implementation. Some ways of authenticating are to send the login and password in the HTTP request header. A site that uses "Basic", "NTLM", or "Digest" authentication uses this scheme. Other sites present a web page containing an HTML form with input elements, where a user must interactively type his username and password and submit. This sends a POST to the web server with the login credentials. Form-based authentication should always occur using a secure TLS connection, otherwise the login credentials are exposed for all to see.

This example shows how to compose an HTTP POST that is equivalent to what a browser would sent when a user clicks on a form submit button. Note: If client-side Javascript is utilized to compute values that are sent as credential, then Chilkat cannot be used to duplicate it.

Suppose you have an HTML form as follows (I've eliminated all HTML tags within the form except for the input tags).

<form method="post" action="/auth.nsf?Login">
<input type="text" size="20" maxlength="256" name="username" id="user-id">
<input type="password" size="20" maxlength="256" name="password" id="pw-id">
<input type="hidden" name="redirectto" value="/web/demo.nsf/pgWelcome?Open">
<input type="submit" value="Log In">
</form>

Imagine the web site is at https://www.something123.com. The path in the "action" attribute of the form tag is the path part of the URL. Therefore, we will POST to https://www.something123.com/auth.nsf?Login

Our HTTP request will contain the following parameters: "username", "password", "redirectto", and optionally "submit". However, it is typically not necessary to add the value of the submit button input field itself. The names of the parameters are found in the "name" attribute of each input field within the HTML form. The value of a hidden input field is in the "value" attribute of the input field.

Note: This is an arbitrary example. A web site can implement forms authentication in any way it sees fit, with any number of form inputs using any names desired. There is no "standard". Every web site can and will implement it differently.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Let's begin by building an HTTP request to mimic the form.  
    -- We must add the parameters, and set the path.
    DECLARE @req int
    EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT

    EXEC sp_OAMethod @req, 'AddParam', NULL, 'username', 'mylogin'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'password', 'mypassword'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'redirectto', '/web/demo.nsf/pgWelcome?Open'

    -- The path part of the POST URL is obtained from the "action" attribute of the HTML form tag.
    EXEC sp_OASetProperty @req, 'Path', '/auth.nsf?Login'

    EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
    EXEC sp_OASetProperty @http, 'FollowRedirects', 1

    -- Collect cookies in-memory and re-send in subsequent HTTP requests, including any redirects.
    EXEC sp_OASetProperty @http, 'SendCookies', 1
    EXEC sp_OASetProperty @http, 'SaveCookies', 1
    EXEC sp_OASetProperty @http, 'CookieDir', 'memory'

    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpSReq', @success OUT, 'www.something123.com', 443, 1, @req, @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    -- The HTTP response object can be examined.
    -- To get the HTML of the response, examine the BodyStr property (assuming the POST returns HTML)
    DECLARE @strHtml nvarchar(4000)
    EXEC sp_OAGetProperty @resp, 'BodyStr', @strHtml OUT

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @req
    EXEC @hr = sp_OADestroy @resp


END
GO