public static String encodeCookie ( Cookie cookie ) { int maxAge = - 1 ; if ( cookie . getExpiryDate ( ) != null ) { maxAge = ( int ) ( ( cookie . getExpiryDate ( ) . getTime ( ) - System . currentTimeMillis ( ) ) / ONE_SECOND ) ; // According to Cookie class specification, a negative value // would be considered as no value. That is not what we want! if ( maxAge < 0 ) { maxAge = 0 ; } } String cookieName = cookie . getName ( ) ; String cookieValue = cookie . getValue ( ) ; StringBuilder buffer = new StringBuilder ( ) ; // Copied from org.apache.http.impl.cookie.BrowserCompatSpec.formatCookies(List<Cookie>) if ( cookie . getVersion ( ) > 0 && ! isQuoteEnclosed ( cookieValue ) ) { buffer . append ( BasicHeaderValueFormatter . INSTANCE . formatHeaderElement ( null , new BasicHeaderElement ( cookieName , cookieValue ) , false ) . toString ( ) ) ; } else { // Netscape style cookies do not support quoted values buffer . append ( cookieName ) ; buffer . append ( "=" ) ; if ( cookieValue != null ) { buffer . append ( cookieValue ) ; } } // End copy appendAttribute ( buffer , "Comment" , cookie . getComment ( ) ) ; appendAttribute ( buffer , "Domain" , cookie . getDomain ( ) ) ; appendAttribute ( buffer , "Max-Age" , maxAge ) ; appendAttribute ( buffer , "Path" , cookie . getPath ( ) ) ; if ( cookie . isSecure ( ) ) { appendAttribute ( buffer , "Secure" ) ; } if ( ( ( BasicClientCookie ) cookie ) . containsAttribute ( HTTP_ONLY_ATTR ) ) { appendAttribute ( buffer , "HttpOnly" ) ; } appendAttribute ( buffer , "Version" , cookie . getVersion ( ) ) ; return ( buffer . toString ( ) ) ; }
Utility method to transform a Cookie into a Set - Cookie Header .