title: How to connect to FTPS server with reuse sessions?
body: <p>I am using the Apache Commons FTP library in my android application.</p>
  <p>I am trying to connect to my FTPS server from my application (using Android Studio) and then upload some files.</p>
  <p>However when I want to reuse sessions by overiding the method <code>_prepareDataSocket_</code> from <code>FTPSClient</code>, I always have the same error about :<code>java.lang.NoSuchFieldException: sessionHostPortCache</code>.
  I have tried with the code from the others posts : <a href="https://stackoverflow.com/questions/32398754/how-to-connect-to-ftps-server-with-data-connection-using-same-tls-session/32404418#32404418">How to connect to FTPS server with data connection using same TLS session?</a>
  or <a href="https://stackoverflow.com/questions/48704243/transfer-files-from-android-with-ftps-to-the-server/48859402#48859402">Transfer files from android with FTPS to the server</a>
  Do you know why I have this error ?</p>
  <p>I'm using Android studio with jdk 1.8.</p>
  <p>I appreciate any help, thanks.</p>
  <h3>Main code :</h3>
  <pre class="lang-java prettyprint-override"><code>String server = &quot;ftp.[HIDDEN]&quot;;
  int port = 21;
  String user = &quot;[HIDDEN]&quot;;
  String pass = &quot;[HIDDEN]&quot;;
  
  System.setProperty(&quot;jdk.tls.useExtendedMasterSecret&quot;, &quot;false&quot;);
  SSLSessionReuseFTPSClient ftpClient = new SSLSessionReuseFTPSClient(&quot;SSL&quot;);
  ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
                  
  
  ftpClient.connect(server, port);
  System.out.println(&quot;Connected to &quot; + server + &quot; on &quot; + port);
  
  ftpClient.login(user, pass);
  
  ftpClient.execPBSZ(0);
  ftpClient.execPROT(&quot;P&quot;);
  ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  ftpClient.enterLocalPassiveMode();
  
  // transfer files
  InputStream input;
  input = new FileInputStream(local);
  ftpClient.storeFile(remote, input);
  input.close();
  
  ftpClient.noop(); // check that control connection is working OK
  ftpClient.logout();
  </code></pre>
  <h3>SSLSessionReuseFTPSClient :</h3>
  <pre class="lang-java prettyprint-override"><code>public class SSLSessionReuseFTPSClient extends FTPSClient {
  
      public SSLSessionReuseFTPSClient(String protocol) {
          super(protocol);
      }
  
      // adapted from: https://trac.cyberduck.io/changeset/10760
      @Override
      protected void _prepareDataSocket_(final Socket socket) throws IOException {
          if (socket instanceof SSLSocket) {
              // Control socket is SSL
              final SSLSession session = ((SSLSocket) _socket_).getSession();
              if (session.isValid()) {
                  final SSLSessionContext context = session.getSessionContext();
                  try {
                      final Field sessionHostPortCache = context.getClass().getDeclaredField(&quot;sessionHostPortCache&quot;);
                      sessionHostPortCache.setAccessible(true);
                      final Object cache = sessionHostPortCache.get(context);
                      final Method method = cache.getClass().getDeclaredMethod(&quot;put&quot;, Object.class, Object.class);
                      method.setAccessible(true);
                      method.invoke(cache, String
                              .format(&quot;%s:%s&quot;, socket.getInetAddress().getHostName(), String.valueOf(socket.getPort()))
                              .toLowerCase(Locale.ROOT), session);
                      method.invoke(cache, String
                              .format(&quot;%s:%s&quot;, socket.getInetAddress().getHostAddress(), String.valueOf(socket.getPort()))
                              .toLowerCase(Locale.ROOT), session);
                  } catch (NoSuchFieldException e) {
                      throw new IOException(e);
                  } catch (Exception e) {
                      throw new IOException(e);
                  }
              } else {
                  throw new IOException(&quot;Invalid SSL Session&quot;);
              }
          }
      }
  }
  </code></pre>
  <h3>Logcat :</h3>
  <pre><code>220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
  07-22 09:53:59.516 9529-9561/com.example.test I/System.out: 220-You are user number 1 of 50 allowed.
  07-22 09:53:59.516 9529-9561/com.example.test I/System.out: 220-Local time is now 11:54. Server port: 21.
  07-22 09:53:59.516 9529-9561/com.example.test I/System.out: 220-This is a private system - No anonymous login
  07-22 09:53:59.516 9529-9561/com.example.test I/System.out: 220-IPv6 connections are also welcome on this server.
  07-22 09:53:59.516 9529-9561/com.example.test I/System.out: 220 You will be disconnected after 15 minutes of inactivity.
  07-22 09:53:59.519 9529-9561/com.example.test I/System.out: AUTH TLS
  07-22 09:53:59.533 9529-9561/com.example.test I/System.out: 234 AUTH TLS OK.
  07-22 09:53:59.561 9529-9550/com.example.test D/EGL_emulation: eglMakeCurrent: 0xadc34d60: ver 2 0 (tinfo 0xadc394d0)
  07-22 09:53:59.589 9529-9550/com.example.test D/EGL_emulation: eglMakeCurrent: 0xadc34d60: ver 2 0 (tinfo 0xadc394d0)
  07-22 09:53:59.593 9529-9561/com.example.test I/System.out: Connected to ftp.[HIDDEN] on 21
  07-22 09:53:59.597 9529-9561/com.example.test I/System.out: USER *******
  07-22 09:53:59.609 9529-9561/com.example.test I/System.out: 331 User [HIDDEN] OK. Password required
  07-22 09:53:59.610 9529-9561/com.example.test I/System.out: PASS *******
  07-22 09:53:59.613 9529-9550/com.example.test D/EGL_emulation: eglMakeCurrent: 0xadc34d60: ver 2 0 (tinfo 0xadc394d0)
  07-22 09:53:59.695 9529-9550/com.example.test D/EGL_emulation: eglMakeCurrent: 0xadc34d60: ver 2 0 (tinfo 0xadc394d0)
  07-22 09:53:59.701 9529-9561/com.example.test I/System.out: 230 OK. Current restricted directory is /
  07-22 09:53:59.702 9529-9561/com.example.test I/System.out: PBSZ 0
  07-22 09:53:59.714 9529-9561/com.example.test I/System.out: 200 PBSZ=0
  07-22 09:53:59.717 9529-9561/com.example.test I/System.out: PROT P
  07-22 09:53:59.727 9529-9561/com.example.test I/System.out: 200 Data protection level set to &quot;private&quot;
  07-22 09:53:59.729 9529-9561/com.example.test I/System.out: TYPE I
  07-22 09:53:59.743 9529-9561/com.example.test I/System.out: 200 TYPE is now 8-bit binary
  07-22 09:53:59.743 9529-9561/com.example.test I/System.out: PASV
  07-22 09:53:59.757 9529-9550/com.example.test D/EGL_emulation: eglMakeCurrent: 0xadc34d60: ver 2 0 (tinfo 0xadc394d0)
  07-22 09:53:59.758 9529-9561/com.example.test I/System.out: 227 Entering Passive Mode (5,134,13,241,188,161)
  07-22 09:53:59.777 9529-9550/com.example.test D/EGL_emulation: eglMakeCurrent: 0xadc34d60: ver 2 0 (tinfo 0xadc394d0)
  07-22 09:53:59.782 9529-9561/com.example.test I/System.out: STOR pictures/test 18.07 1431/test 18.07 1431_19.6.2019_0.32.15.667.jpg
  07-22 09:53:59.792 9529-9561/com.example.test I/System.out: 150 Accepted data connection
  07-22 09:53:59.793 9529-9561/com.example.test W/System.err: java.io.IOException: java.lang.NoSuchFieldException: sessionHostPortCache
  07-22 09:53:59.793 9529-9561/com.example.test W/System.err:     at com.example.test.functions.SSLSessionReuseFTPSClient._prepareDataSocket_(SSLSessionReuseFTPSClient.java:54)
  07-22 09:53:59.793 9529-9561/com.example.test W/System.err:     at org.apache.commons.net.ftp.FTPSClient._openDataConnection_(FTPSClient.java:628)
  07-22 09:53:59.793 9529-9561/com.example.test W/System.err:     at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:653)
  07-22 09:53:59.793 9529-9561/com.example.test W/System.err:     at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:639)
  07-22 09:53:59.793 9529-9561/com.example.test W/System.err:     at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:2030)
  07-22 09:53:59.793 9529-9561/com.example.test W/System.err:     at com.example.test.functions.FTPfunctions2$BackGroundWorker.doInBackground(FTPfunctions2.java:81)
  07-22 09:53:59.793 9529-9561/com.example.test W/System.err:     at com.example.test.functions.FTPfunctions2$BackGroundWorker.doInBackground(FTPfunctions2.java:41)
  07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
  07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     at java.lang.Thread.run(Thread.java:818)
  07-22 09:53:59.794 9529-9561/com.example.test W/System.err: Caused by: java.lang.NoSuchFieldException: sessionHostPortCache
  07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     at java.lang.Class.getDeclaredField(Class.java:890)
  07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     at com.example.test.functions.SSLSessionReuseFTPSClient._prepareDataSocket_(SSLSessionReuseFTPSClient.java:42)
  07-22 09:53:59.794 9529-9561/com.example.test W/System.err:     ... 12 more
  </code></pre>