B4J Question B4J Server breaks Base64 string

KapiteinOllie

New Member
Hello,

I try to sent data encoded with base64 to b4j server but the b4j server replaces the "+" character with " " of the base64 string.

How can i prevent this?

B4X:
Sub Process_Globals
    Private su As StringUtils
    Private ec As B4XCipher
End Sub

Private Sub request
    Dim job As HttpJob
    job.Initialize("", Me)
    
    job.PostString(link&/req, "q="&su.EncodeBase64(ec.Encrypt("client".GetBytes("UTF8"),"server")))
    
    Wait For (job) JobDone (j As HttpJob)
    Log("J: " & j.Success)
    
    If j.Success Then
        MsgboxAsync(j.GetString, "Response")
    End If
End Sub
 
Solution
Use:
B4X:
job.PostString(url, "q=" & su.EncodeUrl(Base64String, "UTF8"))

Server:
Sub Handle (req As ServletRequest, resp As ServletResponse)
    Dim q As String = req.GetParameter("q")
    Log(q)
    Dim su As StringUtils
    Dim b() As Byte = su.DecodeBase64(q)
    Dim r As String = BytesToString(b, 0, b.Length, "UTF8")
    Log(r)
    Dim m As Map = CreateMap("r": q)
    resp.Write(m.As(JSON).ToCompactString)
End Sub

Client:
Sub PostTest
    Dim job As HttpJob
    job.Initialize("", Me)
    Dim su As StringUtils
    Dim value As String = su.EncodeBase64("测试".GetBytes("UTF8"))
    Log(value)
    Dim url As String = "http://127.0.0.1:8080"
    job.PostString(url, "q=" & su.EncodeUrl(value, "UTF8"))
    Wait For (job) JobDone (job As HttpJob)...

hatzisn

Expert
Licensed User
Longtime User
Can you please post here the same EncodeBase64 from B4J and from B4A in order for us not to create new programs to check it... (Also B4i version would be good). You might also want to post the code you use to Decode the data in order to propose some solutions.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Use:
B4X:
job.PostString(url, "q=" & su.EncodeUrl(Base64String, "UTF8"))

Server:
Sub Handle (req As ServletRequest, resp As ServletResponse)
    Dim q As String = req.GetParameter("q")
    Log(q)
    Dim su As StringUtils
    Dim b() As Byte = su.DecodeBase64(q)
    Dim r As String = BytesToString(b, 0, b.Length, "UTF8")
    Log(r)
    Dim m As Map = CreateMap("r": q)
    resp.Write(m.As(JSON).ToCompactString)
End Sub

Client:
Sub PostTest
    Dim job As HttpJob
    job.Initialize("", Me)
    Dim su As StringUtils
    Dim value As String = su.EncodeBase64("测试".GetBytes("UTF8"))
    Log(value)
    Dim url As String = "http://127.0.0.1:8080"
    job.PostString(url, "q=" & su.EncodeUrl(value, "UTF8"))
    Wait For (job) JobDone (job As HttpJob)
    If job.Success Then
        Dim response As Map = job.GetString.As(JSON).ToMap
        Log(response.Get("r"))
    End If
    job.Release
End Sub

B4X:
Server started
5rWL6K+V
测试
 

Attachments

  • b4jserver.zip
    1.3 KB · Views: 15
  • httpclient.zip
    981 bytes · Views: 13
Upvote 0
Solution
Cookies are required to use this site. You must accept them to continue using the site. Learn more…