Android Question Adding CA Bundle for Http POST request

Sreepathy T L

Member
Licensed User
Longtime User
Hi,
I want to upload a file to https url along with some POST parameters:

Current Code:
B4X:
    Dim j As HttpJob
    j.Initialize("j", Me)
  
    Dim fd As MultipartFileData
    fd.Initialize
    fd.KeyName = "xxxxxxx"
    fd.Dir = File.DirInternal
    fd.FileName = "xxxxx.db"
    fd.ContentType = "application/octet-stream"
    j.GetRequest.Timeout = 0
    j.PostMultipart("https://abcd.mydomain.com/application/", CreateMap("uid": "12345","authkey":
"abcdef","appver":"1"), Array(fd))
    ProgressDialogShow2("Uploading data. Please wait...",False)

Sub JobDone(job As HttpJob)
    ProgressDialogHide
    If(job.JobName = "j")Then
        If job.Success Then
                Msgbox("Data uploaded successfully","Success")
        Else
            Msgbox("Error: " & job.ErrorMessage, "Error")
        End If
    End If
End Sub

Now I am getting this error

B4X:
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found

at this line
B4X:
   Msgbox("Error: " & job.ErrorMessage, "Error")


When I checked with openssl
B4X:
openssl s_client -debug -connect abcd.mydomain.com:443 -CAfile .\cacert.pem
it works successfully

cacert.pem is downloaded from https://curl.haxx.se/ca/cacert.pem

So ultimately it is the missing CA bundle in the https request which causes the error.

Question 1: How I can include the CA bundle in the application which will be used during https requests?

I saw the post https://www.b4x.com/android/forum/threads/connecting-to-https-secured-urls.7057/
where Erel has suggested to use Http.InitializeAcceptAll which I think will override https verification. But I could not use it due to error
B4X:
Unknown member initializeacceptall
since j is of type HttpJob.

Question 2: I could not find any example using HttpClient, HttpRequest & HttpResponse to send a post request with file upload. Can anyone please post a simple example?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…