#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim endPoint As String = "https://api.kaspro.id/d5zWSiy5RC"
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
Dim cert As String=$"-----BEGIN CERTIFICATE-----
MIIDpTCCAo0CBF14sw4wDQYJKoZIhvcNAQELBQAwgZYxIzAhBgkqhkiG9w0BCQEW
FGRrYXBzdGVyMDFAZ21haWwuY29tMQswCQYDVQQGEwJJRDEQMA4GA1UECAwHSmFr
YXJ0YTEQMA4GA1UEBwwHSmFrYXJ0YTEeMBwGA1UECgwVRGlnaSBQZXNvbmEgSW5k
b25lc2lhMQswCQYDVQQLDAJJVDERMA8GA1UEAwwIZGthcHN0ZXIwHhcNMTkwOTEx
MDg0MDQ2WhcNMjAwOTExMDg0MDQ2WjCBljEjMCEGCSqGSIb3DQEJARYUZGthcHN0
ZXIwMUBnbWFpbC5jb20xCzAJBgNVBAYTAklEMRAwDgYDVQQIDAdKYWthcnRhMRAw
DgYDVQQHDAdKYWthcnRhMR4wHAYDVQQKDBVEaWdpIFBlc29uYSBJbmRvbmVzaWEx
CzAJBgNVBAsMAklUMREwDwYDVQQDDAhka2Fwc3RlcjCCASIwDQYJKoZIhvcNAQEB
BQADggEPADCCAQoCggEBAK5wStrcZtQJ1OhTEh+VwjWwig+Sh7MUy9ftYeqmByhE
0Jie0CC9O6GBAxaeKrmfA8an0DHlLEZnfEHT3KNmYbImKIZq98eXUJTxkCyCj0nq
fBgwwzx0Vvn4C31sR4EsKXL1sw46Pfba+RaEXW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l8gkTv3aLvR
AXYKkqhkDx144CiqheytuTpFnrnk4xk6fqdaN0aPEHchyfKRk9vxinD+v8PeD+fa
H6Nxy/DqJlEehup82MxdXw=="$
Dim job As HttpJob
DisableStrictMode
job.Initialize("j",Me)
Dim jo As JavaObject
jo.InitializeContext
jo.RunMethod("SetCertificate",Array(cert,pk))
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub DisableStrictMode
Dim jo As JavaObject
jo.InitializeStatic("android.os.Build.VERSION")
If jo.GetField("SDK_INT") > 9 Then
Dim policy As JavaObject
policy = policy.InitializeNewInstance("android.os.StrictMode.ThreadPolicy.Builder", Null)
policy = policy.RunMethodJO("permitAll", Null).RunMethodJO("build", Null)
Dim sm As JavaObject
sm.InitializeStatic("android.os.StrictMode").RunMethod("setThreadPolicy", Array(policy))
End If
End Sub
#if java
import android.content.Context;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.util.Arrays;
import java.util.Collection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.io.ByteArrayInputStream;
import javax.net.ssl.SSLContext;
import android.util.Base64;
import java.io.FileNotFoundException;
import okhttp3.OkHttpClient;
import okhttp3.OkHttpClient.Builder;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
public void SetCertificate(String certificate, String pk) throws
GeneralSecurityException, FileNotFoundException, IOException
{
KeyStore ks;
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(Base64.decode(pk,Base64.DEFAULT));
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
CertificateFactory fact = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) fact.generateCertificate(new ByteArrayInputStream(certificate.getBytes()));
ks = KeyStore.getInstance("PKCS12");
ks.load(null, null);
ks.setKeyEntry("myClientCert", privateKey, "".toCharArray(), new X509Certificate[] {cert});
KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
kmf.init(ks, "".toCharArray());
KeyManager[] keyManagers = kmf.getKeyManagers();
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, null, null);
OkHttpClient client = new OkHttpClient();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslContext.getSocketFactory());
client = builder.build();
Response response = client.newCall(new Request.Builder().get()
.url(_endpoint)
.build()
).execute();
BA.Log("body : " + response.body().string());
BA.Log("code : " + response.code());
}
#End If