@@ -347,7 +347,7 @@ private BoxAPIResponse trySend(ProgressListener listener) {
347347 }
348348
349349 if (this .api != null ) {
350- connection .addRequestProperty ("Authorization" , "Bearer " + this .api .getAccessToken ());
350+ connection .addRequestProperty ("Authorization" , "Bearer " + this .api .lockAccessToken ());
351351 connection .setRequestProperty ("User-Agent" , this .api .getUserAgent ());
352352
353353 if (this .api instanceof SharedLinkAPIConnection ) {
@@ -363,26 +363,34 @@ private BoxAPIResponse trySend(ProgressListener listener) {
363363 }
364364
365365 this .requestProperties = connection .getRequestProperties ();
366- this .writeBody (connection , listener );
367366
368- // Ensure that we're connected in case writeBody() didn't write anything.
367+ int responseCode ;
369368 try {
370- connection .connect ();
371- } catch (IOException e ) {
372- throw new BoxAPIException ("Couldn't connect to the Box API due to a network error." , e );
373- }
369+ this .writeBody (connection , listener );
374370
375- this .logRequest (connection );
371+ // Ensure that we're connected in case writeBody() didn't write anything.
372+ try {
373+ connection .connect ();
374+ } catch (IOException e ) {
375+ throw new BoxAPIException ("Couldn't connect to the Box API due to a network error." , e );
376+ }
376377
377- // We need to manually handle redirects by creating a new HttpURLConnection so that connection pooling happens
378- // correctly. There seems to be a bug in Oracle's Java implementation where automatically handled redirects will
379- // not keep the connection alive.
380- int responseCode ;
381- try {
382- responseCode = connection .getResponseCode ();
383- } catch (IOException e ) {
384- throw new BoxAPIException ("Couldn't connect to the Box API due to a network error." , e );
378+ this .logRequest (connection );
379+
380+ // We need to manually handle redirects by creating a new HttpURLConnection so that connection pooling
381+ // happens correctly. There seems to be a bug in Oracle's Java implementation where automatically handled
382+ // redirects will not keep the connection alive.
383+ try {
384+ responseCode = connection .getResponseCode ();
385+ } catch (IOException e ) {
386+ throw new BoxAPIException ("Couldn't connect to the Box API due to a network error." , e );
387+ }
388+ } finally {
389+ if (this .api != null ) {
390+ this .api .unlockAccessToken ();
391+ }
385392 }
393+
386394 if (isResponseRedirect (responseCode )) {
387395 return this .handleRedirect (connection , listener );
388396 }
0 commit comments