Android Question HttpUtils2 GetHeaders

Rick Harris

Well-Known Member
Licensed User
I have searched the forum but cannot find any solution to get headers by means of GetHeaders using HttpUtils2. I need to access the contents of headers in a SOAP project.
 

Erel

Administrator
Staff member
Licensed User
HttpUtils2 doesn't expose the headers. Use HttpUtils2 source code instead of the compiled library and modify hc_ResponseSuccess with these two lines:
B4X:
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Dim job As HttpJob = TaskIdToJob.Get(TaskId)
   job.Tag = Response.GetHeaders
   
   Response.GetAsynchronously("response", File.OpenOutput(TempFolder, TaskId, False), _
     True, TaskId)
End Sub
Job.tag will now include a Map with the headers.
 

Rick Harris

Well-Known Member
Licensed User
Thank you. Yes, in the mean time I also found this solution elsewhere on the forum, and it works fine.
It would still be recommendable to add a "GetHeaders" function to the HttpUtils2 library itself.
 

Rick Harris

Well-Known Member
Licensed User
Repeated request to Erel: Please add "GetHeaders" to the HttpUtils2 library.
I have added code myself to the .bas module and it works but I am now getting strange, unexplainable "Android Thread" errors elsewhere in my project. Here is my code:

B4X:
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
  Try
    Response.GetAsynchronously("response", File.OpenOutput(TempFolder, TaskId, False), True, TaskId)
 
    'Log("HttpClient1_ResponseSuccess")
    'Log("TaskId: "& TaskId)
    'Log("** Headers **")
 
    Dim Headers As Map=Response.GetHeaders
    Dim i As Int
    Dim Key, Value As String
    For i=0 To Headers.Size-1
      Key=Headers.GetKeyAt(i)
      Value=Headers.GetValueAt(i)
      If Key="Set-Cookie" Then         
        Main.Cookie1 = Value
        CallSub(Main,"SET_COOKIE1")
        Log(Key &" : " & Value)
      End If
    Next
 
   Response.Release                     
  Catch
  End Try
End Sub
Note: This code also shows Google's attemp to place a Cookie on my device (in the Log).
The SET_COOKIE1 sub routine in the Main module has nothing to do with Cookies, but is used to obtain a Key to access a SOAP server. This works also.
 
Last edited:
Top