Sub Process_Globals
Type responseLocationAndCookie(location As String,cookie As String)
End Sub
Sub Globals
Public hc As OkHttpClient
Private strUtils As StringUtils
Private tempFolder As String
Private myCookiesMap As Map
End Sub
Sub Activity_Create(FirstTime As Boolean)
tempFolder=File.DirInternalCache
connectHttp
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub connectHttp
Log(DateTime.Now)
If myCookiesMap.IsInitialized=False Then
myCookiesMap.Initialize
End If
myCookiesMap.Clear
Dim jo As JavaObject = hc
Dim builder As JavaObject = jo.RunMethod("sharedInit", Array("hc"))
builder.RunMethod("followRedirects", Array(False))
builder.RunMethod("followSslRedirects",Array(False))
jo.SetField("client", builder.RunMethod("build", Null))
Private req1 As OkHttpRequest
req1.InitializeGet("https://www1.gsis.gr/taxisnet/mytaxisnet/protected/home.htm")
hc.Execute(req1,0)
End Sub
Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
Private temptype As responseLocationAndCookie=getLocationAndCookieFromResponse(Response)
Response.GetAsynchronously("response",File.OpenOutput(tempFolder,TaskId,False),True,TaskId)
End Sub
Sub hc_ResponseError (Response As OkHttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
If Floor(StatusCode / 100) = 3 Then
If Response <> Null Then
Private temptype As responseLocationAndCookie=getLocationAndCookieFromResponse(Response)
If temptype.location.Length>0 Then
TaskId=TaskId+1
Private req As OkHttpRequest
req.InitializeGet(temptype.location)
Private tempcookie As String=prepairCookie
req.SetHeader("Cookie",tempcookie)
hc.Execute(req,TaskId)
Else
Log("error: no location for redirect set")
End If
Else
Log("error: response null")
End If
Else
Log("error: "&Reason&","&StatusCode)
End If
End Sub
Sub response_StreamFinish (Success As Boolean, TaskId As Int)
Select True
Case TaskId>0
Private token As String
Private html0 As String=decompressToString(File.OpenInput(tempFolder,TaskId),"UTF8",getString(TaskId))
Private searchToken As String=$"request_id" value=""$
Private startIndexToken As Int=html0.IndexOf(searchToken)
If startIndexToken>-1 Then
startIndexToken=startIndexToken+searchToken.Length
Private endIndexToken As Int=html0.IndexOf2($"""$,startIndexToken)
If endIndexToken>startIndexToken Then
token=html0.SubString2(startIndexToken,endIndexToken)
token=strUtils.EncodeUrl(token.Replace("-","-"),"UTF8")
End If
End If
Private username As String="ausername"
Private userpass As String="apassword"
username=strUtils.EncodeUrl(username,"UTF8")
userpass=strUtils.EncodeUrl(userpass,"UTF8")
token=strUtils.EncodeUrl(token,"UTF8")
Private postData As String=$"username=${username}&password=${userpass}&request_id=${token}&btn_login="$
'----------
clearHttp: 'if I don't clear cookies I get the same error as in my original code
'----------
Private req2 As OkHttpRequest
Private tempBytes() As Byte=postData.GetBytes("UTF8")
req2.InitializePost2("https://mygovlogin.gsis.gr/oam/server/auth_cred_submit",tempBytes)
Private tempcookie As String=prepairCookie
req2.SetHeader("Cookie",tempcookie)
hc.Execute(req2,-1)
Case Else
Log(DateTime.Now)
Private html1 As String=decompressToString(File.OpenInput(tempFolder,TaskId),"UTF8",getString(TaskId))
Log(html1)
End Select
End Sub
Sub getLocationAndCookieFromResponse(response As OkHttpResponse) As responseLocationAndCookie
Private tempType As responseLocationAndCookie
tempType.Initialize
Private map0 As Map=response.GetHeaders
Private locations As List=map0.Get("location")
Private location As String
If locations.IsInitialized Then
location=locations.Get(0)
End If
tempType.location=location
For Each key As String In map0.Keys
Private tempVal As List=map0.Get(key)
For Each st As String In tempVal
If key.ToLowerCase.StartsWith("set-cookie") Then
Private tempValStart As Int=st.IndexOf("=")
Private tempCookieKey As String=st.SubString2(0,tempValStart)
tempValStart=tempValStart+1
Private tempCookieVal As String=st.SubString2(tempValStart,st.IndexOf2(";",tempValStart))
If tempCookieVal.Length>0 Then
myCookiesMap.Put(tempCookieKey,tempCookieVal)
End If
End If
Next
Next
Return tempType
End Sub
Sub prepairCookie As String
Private newCookie As String
For Each cookieName As String In myCookiesMap.Keys
Private cookieVal As String=myCookiesMap.Get(cookieName)
newCookie=newCookie&cookieName&"="&cookieVal&";"
Next
Return newCookie
End Sub
Sub clearHttp
Dim jo As JavaObject =hc
jo = jo.GetFieldJO("client").RunMethod("cookieJar", Null)
Dim r As Reflector
r.Target = jo
Dim CookieManager As JavaObject = r.GetField("cookieHandler")
CookieManager.RunMethodJO("getCookieStore", Null).RunMethod("removeAll", Null)
End Sub
Sub getString(taskID As Int) As String
Dim tr As TextReader
tr.Initialize(File.OpenInput(tempFolder, taskID))
Dim res As String = tr.ReadAll
tr.Close
Return res
End Sub
Sub decompressToString(tempInputStream As InputStream,encoding As String,defaultString As String) As String
Dim OS As OutputStream
OS.InitializeToBytesArray(1000)
File.Copy2(tempInputStream, OS)
Dim Buffer() As Byte
Buffer = OS.ToBytesArray
Dim compress As CompressedStreams
Dim decompressed() As Byte
Dim DecompressedString As String
Try
decompressed = compress.DecompressBytes(Buffer, "gzip")
DecompressedString=BytesToString(decompressed, 0, decompressed.Length, encoding)
Catch
DecompressedString=defaultString
End Try
Return DecompressedString
End Sub