CALL an ASHX Handler

wizard699

Active Member
Licensed User
Longtime User
I've created an handler that call a web service, to obtain an JSON RPC response.
The handler have a method1 with 3 parameters and, calling from browser, all goes well.

How can call this ASHX from B4A, calling the METHOD1 and passing 3 parameters?

Thanks in advance,
Wizard
 

COBRASoft

Active Member
Licensed User
Longtime User
Check HttpUtils (search). You can use either POST or GET according to how your ASHX is configured. I've tested both and it works great.
 
Upvote 0

COBRASoft

Active Member
Licensed User
Longtime User
If you could provide me the URL to your ASHX, I could make an example. Be carefull, the parameters are case sensitive. If you use POST, you have to use a MAP and the JSonGenerator to form the parameters (headers).

I also had to make a change to HttpUtils and HttpUtilsService so they use the correct 'ContentType'. In the 'PostString' and 'PostBytes' methods, you have to add the 'ContentType' parameter (see code snippets below).

HttpUtils:
B4X:
Sub PostString(JobName As String, URL As String, Text As String, ContentType As String)
    PostBytes(JobName, URL, Text.GetBytes("UTF8"), ContentType)
End Sub

Sub PostBytes(JobName As String, URL As String, Data() As Byte, ContentType As String)
    If internalCheckIfCanStart(JobName) = False Then Return
    HttpUtilsService.PostInputStream = Null
    HttpUtilsService.PostBytes = Data
    HttpUtilsService.ContentType = ContentType
    Tasks = Array As String(URL)
    HttpUtilsService.Post = True
    StartService(HttpUtilsService)
End Sub
HttpUtilsService:
B4X:
Sub Process_Globals
    '--- Under the existing lines you will find here..
    Dim ContentType As String '--- Add this line
End Sub

Sub ProcessNextTask
    If finishTasks >= HttpUtils.Tasks.Size Then
        HttpUtils.Working = False
        HttpUtils.Complete = True
        'Raise job done event
        If HttpUtils.CallbackJobDoneSub <> "" Then
            CallSub2(HttpUtils.CallbackActivity, HttpUtils.CallbackJobDoneSub, HttpUtils.Job)
        End If
        'If no new job was started then we kill this service.
        If HttpUtils.Working = False Then
            StopService("")
        End If
        Return
    End If
    If task >= HttpUtils.Tasks.Size Then Return
    Dim link As String
    link = HttpUtils.Tasks.Get(task)
    Dim req As HttpRequest
    If Post Then
        If PostInputStream.IsInitialized Then
            req.InitializePost(link, PostInputStream, PostLength)
        Else
            req.InitializePost2(link, PostBytes)
            req.SetContentType(ContentType) '--- Add this line
        End If
    Else
        req.InitializeGet(link)
    End If
    'Here you can modify the request object if needed
    countWorking = countWorking + 1
    taskToRequest.Put(task, link)
    hc.Execute(req, task)
    task = task + 1
End Sub
Sample:
B4X:
    Dim ContentType As String: ContentType = "application/json"

    Dim objMap As Map: objMap.Initialize
    objMap.Put("DEVICENAME", SomeDeviceName)
    Dim objJSon As JSONGenerator: objJSon.Initialize(objMap)
    HttpUtils.PostString("SomeString", "YourASHXURL", objJSon.ToString, ContentType)
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…