public static Map < String , String > getQueryKeyValuePairs ( URI uri ) throws UnsupportedEncodingException { Map < String , String > queryParameters = new HashMap < String , String > ( ) ; String query = uri . getRawQuery ( ) ; if ( query == null ) { // Maybe invalid URI try { uri = URI . create ( uri . getRawSchemeSpecificPart ( ) ) ; query = uri . getRawQuery ( ) ; if ( query == null ) { return queryParameters ; } } catch ( IllegalArgumentException ex ) { return queryParameters ; } } StringTokenizer stringTokenizer = new StringTokenizer ( query , "&" ) ; while ( stringTokenizer . hasMoreTokens ( ) ) { String keyValue = stringTokenizer . nextToken ( ) . trim ( ) ; if ( ! keyValue . isEmpty ( ) ) { int equalPos = keyValue . indexOf ( "=" ) ; // If there is no value String key = URLDecoder . decode ( keyValue . substring ( 0 , equalPos != - 1 ? equalPos : keyValue . length ( ) ) , ENCODING ) ; if ( equalPos == - 1 || equalPos == keyValue . length ( ) - 1 ) { // Key without value queryParameters . put ( key . toLowerCase ( ) , "" ) ; } else { String value = URLDecoder . decode ( keyValue . substring ( equalPos + 1 ) , ENCODING ) ; queryParameters . put ( key . toLowerCase ( ) , value ) ; } } } return queryParameters ; }
Read the Query part of an URI .