SAP/ASP

Asp HTTP Handler for MSSql Query :

The Handler Factory:

 vbnet |  copy code |? 
Imports Microsoft.VisualBasic
Imports System.Web
Class HandlerFactory
    Implements IHttpHandlerFactory
    Public Function GetHandler(ByVal context As HttpContext, _
            ByVal requestType As String, ByVal url As [String], _
            ByVal pathTranslated As [String]) As IHttpHandler _
            Implements IHttpHandlerFactory.GetHandler
        Dim handlerToReturn As IHttpHandler
        handlerToReturn = New SapQ()
     
        Return handlerToReturn
    End Function
    Public Sub ReleaseHandler(ByVal handler As IHttpHandler) _
        Implements IHttpHandlerFactory.ReleaseHandler
    End Sub
    Public ReadOnly Property IsReusable() As Boolean
        Get
            Return False
        End Get
    End Property
End Class

The asyncronous HTTP Handler:

 vbnet |  copy code |? 
Imports Microsoft.VisualBasic
Imports System.Web
Imports System.Threading
Imports System.Data.SqlClient
Public Class SapQ
    Implements IHttpAsyncHandler
    Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
    Public Function BeginProcessRequest( _
        ByVal context As System.Web.HttpContext, _
        ByVal cb As System.AsyncCallback, _
        ByVal extraData As Object) _
        As System.IAsyncResult _
        Implements System.Web.IHttpAsyncHandler.BeginProcessRequest
        Dim asynch As New AsynchOperation(cb, context, extraData)
        asynch.StartAsyncWork()
        Return asynch
    End Function
    Public Sub EndProcessRequest(ByVal result As  _
         System.IAsyncResult) _
         Implements System.Web.IHttpAsyncHandler.EndProcessRequest
    End Sub
    Public Sub ProcessRequest(ByVal context _
            As System.Web.HttpContext) _
            Implements System.Web.IHttpHandler.ProcessRequest
        Throw New InvalidOperationException()
    End Sub
End Class
Class AsynchOperation
    Implements IAsyncResult
    Private _completed As Boolean
    Private _state As [Object]
    Private _callback As AsyncCallback
    Private _context As HttpContext
    ReadOnly Property IsCompleted() As Boolean _
            Implements IAsyncResult.IsCompleted
        Get
            Return _completed
        End Get
    End Property
    ReadOnly Property AsyncWaitHandle() As WaitHandle _
            Implements IAsyncResult.AsyncWaitHandle
        Get
            Return Nothing
        End Get
    End Property
    ReadOnly Property AsyncState() As [Object] _
            Implements IAsyncResult.AsyncState
        Get
            Return _state
        End Get
    End Property
    ReadOnly Property CompletedSynchronously() As Boolean _
            Implements IAsyncResult.CompletedSynchronously
        Get
            Return False
        End Get
    End Property
    Public Sub New(ByVal callback As AsyncCallback, _
            ByVal context As HttpContext, _
            ByVal state As [Object])
        _callback = callback
        _context = context
        _state = state
        _completed = False
    End Sub
    Public Sub StartAsyncWork()
        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf StartAsyncTask), Nothing)
    End Sub
    
Private Sub StartAsyncTask(ByVal workItemState As [Object])
    Dim request As HttpRequest = _context.Request
    Dim response As HttpResponse = _context.Response
    Dim connectString As String
    Dim sqlCnn As New SqlConnection
    Dim sqlCmd As SqlCommand
        Dim Sql As String
    '*** HTTP Header values
        Dim Coll As NameValueCollection
        '*** Table values
        Dim Tx() As String
        ' *** CONNECT ****************************************
        ' Get the ConnectionStrings from POST Form value
        Coll = request.Form
        connectString = Coll.Get("SQLConnectionString")
        ' Get the ConnectionStrings from Web.config
        If connectString = "" Then
            ' Get the ConnectionStrings collection. (web.config)
            Dim connections As ConnectionStringSettingsCollection = ConfigurationManager.ConnectionStrings
            connectString = ConfigurationManager.ConnectionStrings("SQLConnectionString").ConnectionString
        End If
        ' Get the ConnectionStrings Direct
        If connectString = "" Then
            sqlCnn.ConnectionString = "Server=localhost;Database=master;User ID=sa;Password=xxx"
        End If
        ' ***Sql Command *************************************
        ' Get the SQL from POST Form value
        Sql = Coll.Get("SQLCommandString")
        ' Get the SQL Direct
        If Sql = "" Then
           'lists ALL Databases and their MDF-files 
            Sql = "SELECT name, filename FROM master..sysdatabases"
            'lists all Tables of a Database 
            'Sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'"
            'lists all VIEWS of a Database
            'Sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'VIEW'"
            'lists the column name with the according datatypes of a Table
            'Sql = "SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'SAMPLE'"
        End If
        ' ***Sql EXECUTE ************************************************************************************
        sqlCnn.ConnectionString = connectString
        Try
            sqlCnn.Open()
            sqlCmd = New SqlCommand(Sql, sqlCnn)
            Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader()
            ' get numer of fields
            Dim fCount As Integer = sqlReader.FieldCount
            Dim i As Integer
            ReDim Tx(0)
            While sqlReader.Read()
                If fCount > 1 Then
                    For i = 0 To fCount - 2
                        Tx(UBound(Tx)) = Tx(UBound(Tx)) & sqlReader.Item(i).ToString & vbTab
                    Next i
                    Tx(UBound(Tx)) = Tx(UBound(Tx)) & sqlReader.Item(i).ToString
                Else
                    Tx(UBound(Tx)) = sqlReader.Item(0).ToString
                End If
                ReDim Preserve Tx(UBound(Tx) + 1)
            End While
            sqlReader.Close()
            sqlCmd.Dispose()
            sqlCnn.Close()
            For i = 0 To UBound(Tx) - 1
                response.Write(Tx(i) & vbLf)
            Next i
        Catch ex As Exception
            response.Write(ex.Message)
        End Try
        _completed = True
        _callback(Me)
    End Sub 'StartAsyncTask
End Class 'AsynchOperation

The web.config:

 html |  copy code |? 


  
    
  
  
    
      
    
  
  
    
  

ABAP Calling Example:

 abap |  copy code |? 
*&---------------------------------------------------------------------*
*& Report  YBC_HTTP_SERVER_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  ybc_http_dw_client_test.
CONSTANTS %lf(1) VALUE %_newline.
* data declarations
DATA: client TYPE REF TO if_http_client.
DATA:
host TYPE string VALUE 'http://dwx61/sapq',
errortext TYPE string.
DATA:
uri TYPE string,
subrc TYPE i,
version TYPE i,
request TYPE REF TO if_http_request.
DATA res_data TYPE xstring.
DATA res_cdata TYPE string.
DATA:buffer TYPE xstring,
     conv TYPE REF TO cl_abap_conv_in_ce,
     buf TYPE TABLE OF tab512 WITH HEADER LINE,
     bytes_read TYPE i,
     fname TYPE string VALUE 'c:\temp\000.html'.
DEFINE m_err_exit.
  if sy-subrc <> 0.
    call method client->get_last_error
      importing
        code    = subrc
        message = errortext.
    exit.
  endif.
END-OF-DEFINITION.
CALL METHOD cl_http_client=>create_by_url
  EXPORTING
    url                = host
  IMPORTING
    client             = client
  EXCEPTIONS
    argument_not_found = 1
    plugin_not_active  = 2
    internal_error     = 3
    OTHERS             = 4.
m_err_exit.
client->request->set_header_field( name = '~request_method'
value = 'POST' ).
CALL METHOD client->request->set_form_field
  EXPORTING
    name  = 'SQLConnectionString'
    value = 'Server=localhost;Database=master;User ID=sa;Password=xxx'.
CALL METHOD client->request->set_form_field
  EXPORTING
    name  = 'SQLCommandString'
    value = 'SELECT name, filename FROM master..sysdatabases'.
CALL METHOD client->send
*exporting timeout = timeout
  EXCEPTIONS http_communication_failure = 1
    http_invalid_state = 2
    http_processing_failed = 3
    OTHERS = 4.
m_err_exit.
CALL METHOD client->receive
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2
    http_processing_failed     = 3
    OTHERS                     = 4.
m_err_exit.
CALL METHOD client->response->get_data
  RECEIVING
    data = res_data.
m_err_exit.
conv = cl_abap_conv_in_ce=>create( encoding = 'UTF-8' input = res_data ).
conv->read( IMPORTING data = res_cdata ).
FREE conv.
DATA res_tab TYPE TABLE OF tab512 WITH HEADER LINE.
SPLIT res_cdata AT %lf INTO TABLE res_tab.
LOOP AT res_tab.
  WRITE :/ res_tab-wa.
ENDLOOP.
CALL METHOD client->close
  EXCEPTIONS
    http_invalid_state = 1
    OTHERS             = 2.
m_err_exit.