Skip to content

HTTP Request Customization in .NET Android

For the Android platform for .NET MAUI, you will be required to conform to INetworkInterceptorListener which provides you with a callback that receives HttpRequestBuilder right before requests are fired. In this example, we will utilize FaceSdkInit to intercept our requests.

If you want to intercept HTTP requests, make sure to specify the listener before startLiveness or matchFaces:

using Com.Regula.Facesdk.Callback;
using Com.Regula.Facesdk.Listener;
using Com.Regula.Facesdk.Exception;
using Com.Regula.Facesdk;
using FaceSample.Platforms.Android;
using Com.Regula.Common.Http;

[assembly: Xamarin.Forms.Dependency(
          typeof(FaceSdkInit))]
namespace FaceSample.Platforms.Android
{
    public class FaceSdkInit : Java.Lang.Object, IFaceSdkInit, IInitCallback, INetworkInterceptorListener
    {
        public FaceSdkInit()
        {
        }

        public void InitFaceSdk()
        {
            FaceSDK.Instance().Initialize(Android.App.Application.Context, this);
        }

        public void OnInitCompleted(bool success, InitException error)
        {
            FaceSDK.Instance().SetNetworkInterceptorListener(this);
        }

        void INetworkInterceptorListener.OnPrepareRequest(HttpRequestBuilder httpRequestBuilder)
        {
            var authorization = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmYWNlLXNkayI6IkhhdmUgYSBncmVhdCBkYXkhIn0.IPoW0D0LnMv_pL4U22MuIhDNGIdK34TaHhqhKBAaBEs";
            httpRequestBuilder.Connection.SetRequestProperty("Authorization", authorization);
        }
    }
}