@@ -87,7 +87,7 @@ protected CookieInterceptorBase(String baseUrl, String endpoint, String requestM
8787 */
8888 protected abstract byte [] getSessionRequestPayload (HttpConnectionInterceptorContext context );
8989
90- private void requestCookie (HttpConnectionInterceptorContext context ) {
90+ private void requestCookie (HttpConnectionInterceptorContext context ) throws IOException {
9191 // Check if the session was already updated on another thread before getting a cookie
9292 sessionLock .readLock ().lock ();
9393 try {
@@ -99,7 +99,14 @@ private void requestCookie(HttpConnectionInterceptorContext context) {
9999 if (sessionId .equals (context .getState (this , sessionStateName , UUID .class ))) {
100100 HttpConnection sessionConn = makeSessionRequest (sessionRequestUrl ,
101101 getSessionRequestPayload (context ), sessionRequestMimeType , context );
102- storeCookiesFromResponse (sessionConn .getConnection ());
102+ HttpURLConnection sessionUrlConnection = sessionConn .getConnection ();
103+ try {
104+ storeCookiesFromResponse (sessionUrlConnection );
105+ } finally {
106+ // We use collect rather than consume as we don't want to log
107+ // a warning, even though we don't actually need the body
108+ Utils .collectAndCloseStream (sessionUrlConnection .getInputStream ());
109+ }
103110 // We renewed a cookie, update the global sessionID and this request's context
104111 sessionId = UUID .randomUUID ();
105112 context .setState (this , sessionStateName , sessionId );
@@ -163,21 +170,20 @@ public HttpConnectionInterceptorContext interceptRequest(HttpConnectionIntercept
163170 // Set the sessionId for this request
164171 context .setState (this , sessionStateName , sessionId );
165172 HttpURLConnection connection = context .connection .getConnection ();
173+ try {
174+ // First time we will have no cookies
175+ if (cookieManager .getCookieStore ().getCookies ().isEmpty ()) {
176+ requestCookie (context );
177+ }
166178
167- // First time we will have no cookies
168- if (cookieManager .getCookieStore ().getCookies ().isEmpty ()) {
169- requestCookie (context );
170- }
171-
172- // Debug logging
173- if (logger .isLoggable (Level .FINEST )) {
174- logger .finest ("Attempt to add cookie to request." );
175- logger .finest ("Cookies are stored for URIs: " + cookieManager .getCookieStore ()
176- .getURIs ());
177- }
179+ // Debug logging
180+ if (logger .isLoggable (Level .FINEST )) {
181+ logger .finest ("Attempt to add cookie to request." );
182+ logger .finest ("Cookies are stored for URIs: " + cookieManager .getCookieStore ()
183+ .getURIs ());
184+ }
178185
179- // Apply any saved cookies to the request
180- try {
186+ // Apply any saved cookies to the request
181187 Map <String , List <String >> requestCookieHeaders = cookieManager .get (connection
182188 .getURL ().toURI (), connection .getRequestProperties ());
183189 for (Map .Entry <String , List <String >> requestCookieHeader :
@@ -217,7 +223,7 @@ public HttpConnectionInterceptorContext interceptResponse(HttpConnectionIntercep
217223 context .replayRequest = true ;
218224 }
219225 } catch (IOException e ) {
220- throw wrapIOException ("Failed to read HTTP reponse code or body from" , connection , e );
226+ throw wrapIOException ("Failed to read HTTP response code or body from" , connection , e );
221227 }
222228 return context ;
223229 }
0 commit comments