Enabling OkHttp for tunneling

Last published : Oct 31, 2026
Ensure that you add this method call right before calling call.execute(…).
The MicroVPNSDK.enableOkHttpClientObjectForNetworkTunnel(…) method is used to update an OkHttpClient object before doing a network call. After the network tunnel is established successfully, call the MicroVPNSDK.enableOkHttpClientObjectForNetworkTunnel(…) method before calling the okHttpClient.newCall(request).execute() method. This method returns the modified OkHttpClient object that is enabled for tunneling. Since the #startTunnel(…) method is an asynchronous call, the tunnel needs to be started before calling this method. Otherwise it throws a NetworkTunnelNotStartedException exception.
Note:
Do not update the OkHttpClient object after the #enableOkHttpClientObjectForNetworkTunnel(…) method call. Citrix recommends calling the MicroVPNSDK.enableOkHttpClientObjectForNetworkTunnel(…) method right before the call.execute() method.
try {
    OkHttpClient client = new OkHttpClient.Builder().build();
    Request request = new Request.Builder().url(url).build();
    client = (OkHttpClient) MicroVPNSDK.enableOkHttpClientObjectForNetworkTunnel(context, client);
    Response response = client.newCall(request).execute();
    responseHtml = response.body().string();
} catch(NetworkTunnelNotStartedException nse) {
    Log.e(LOG_TAG, nse.getMessage());
} catch(Exception e) {
    Log.e(LOG_TAG, e.getMessage());
}