Android Code Snippet ServletRequest all Header and Parameters (httpServer)

I arose from the need to know all the headers of a GET request.

When HttServer raises the HttpServer_HandleRequest event it returns the Request variable.

The Request variable has only one method for reading Headers, GetHeader.
How to get a complete list of Headers?

B4X:
    HttpServer1.Initialize("HttpServer")
    HttpServer1.Start(1080)

B4X:
Private Sub HttpServer_HandleRequest (Request As ServletRequest, Response As ServletResponse)
    If Request.Method = "GET" Then
        Dim ServletRequestWrapper As Reflector
        ServletRequestWrapper.Target=Request
      
        Dim req As JavaObject = ServletRequestWrapper.GetField("req")
        Dim headerNames As JavaObject = req.RunMethod("getHeaderNames",Null)

         ' List of Header
        Do While headerNames.RunMethod("hasMoreElements",Null)
            Log(headerNames.RunMethod("nextElement",Null)) ' Header
        Loop
    End If

End Sub

NB Requires JavaObject and Reflector library
 

Star-Dust

Expert
Licensed User
Longtime User

ServletRequest all Parameters​

B4X:
Private Sub GetParameterNames(Request As ServletRequest) As List
    Dim ServletRequestWrapper As Reflector
    
    Dim L As List
    L.Initialize
    ServletRequestWrapper.Target=Request
      
    Dim req As JavaObject = ServletRequestWrapper.GetField("req")
    Dim ParameterNames As JavaObject = req.RunMethod("getParameterNames",Null)

    ' List of Header
    Do While ParameterNames.RunMethod("hasMoreElements",Null)
        l.Add(ParameterNames.RunMethod("nextElement",Null)) ' Parameter
    Loop
        
    Return L
End Sub

If you want a Map
B4X:
Private Sub GetParameterNames(Request As ServletRequest) As Map
    Dim ServletRequestWrapper As Reflector

    ServletRequestWrapper.Target=Request
      
    Dim req As JavaObject = ServletRequestWrapper.GetField("req")
    Dim ParameterMap As Map = req.RunMethod("getParameterMap",Null)
    Return ParameterMap
End Sub
 
Top