I try for 2 weeks without success.
The requirement is that: POST method, Header acceptance: application / json, Content-Type: application / json. Authentication: Use the client's digital certificate for authentication with each request to Api. Client digital certificate: .pfk file and two .cer files (RCA and CA 1).
I made an example in vb.net and it works.
Is that possible with b4a?
The requirement is that: POST method, Header acceptance: application / json, Content-Type: application / json. Authentication: Use the client's digital certificate for authentication with each request to Api. Client digital certificate: .pfk file and two .cer files (RCA and CA 1).
I made an example in vb.net and it works.
Is that possible with b4a?
ASP.net:
Imports System.IO
Imports System.Net
Imports System.Net.Http
Imports System.Security.Cryptography.X509Certificates
Imports System.Text
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim invoiceRequest As String = My.Computer.FileSystem.ReadAllText("C:\Users\Public\test.txt") 'JSON structure
Dim httpContent = New StringContent(invoiceRequest, Encoding.UTF8, "application/json")
Dim client As New HttpClient
Dim handler As New WebRequestHandler
GetClientAndHandler(handler, client)
Dim response = client.PostAsync("/api/Sign/Service", httpContent).Result
If response.StatusCode = HttpStatusCode.OK Then
Dim jsonString = response.Content.ReadAsStringAsync()
jsonString.Wait()
Dim invoiceResponse = jsonString.Result
TextBox2.Text = invoiceResponse
Else
TextBox2.Text = "HTTP status not OK, status: " + response.StatusCode.ToString
End If
End Sub
Private Shared Sub GetClientAndHandler(ByRef handler As WebRequestHandler, ByRef client As HttpClient)
handler = CreateWebRequestHandler()
client = New HttpClient(handler)
client.BaseAddress = New Uri("https://xxxx.xxxx.xx.xxx.xx.xx/")
client.DefaultRequestHeaders.Accept.Clear()
End Sub
Private Shared Function CreateWebRequestHandler() As WebRequestHandler
Dim handler = New WebRequestHandler()
Dim cert = GetClientCertificate()
handler.ClientCertificateOptions = ClientCertificateOption.Manual
handler.ClientCertificates.Add(cert)
Return handler
End Function
Private Shared Function GetClientCertificate() As X509Certificate2
Dim certName As String = "xxname certxx"
Dim store = New X509Store(StoreName.My, StoreLocation.CurrentUser)
store.Open(OpenFlags.OpenExistingOnly Or OpenFlags.[ReadOnly])
Return store.Certificates.Find(X509FindType.FindBySubjectName, certName, True)(0)
End Function