code: protected static CharSequence encodeUriQuery(CharSequence in)
     {
        /*
         * Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things. TODO:
         * replace/compare to with Rewrite Encoding
         */
        StringBuilder outBuf = null;
        Formatter formatter = null;
        for (int i = 0; i < in.length(); i++)
        {
           char c = in.charAt(i);
           boolean escape = true;
           if (c < 128)
           {
              if (asciiQueryChars.get(c))
              {
                 escape = false;
              }
           }
           else if (!Character.isISOControl(c) && !Character.isSpaceChar(c))
           {
              /*
               * not-ascii
               */
              escape = false;
           }
           if (!escape)
           {
              if (outBuf != null)
                 outBuf.append(c);
           }
           else
           {
              /*
               * escape
               */
              if (outBuf == null)
              {
                 outBuf = new StringBuilder(in.length() + 5 * 3);
                 outBuf.append(in, 0, i);
                 formatter = new Formatter(outBuf);
              }
              /*
               * leading %, 0 padded, width 2, capital hex
               */
              formatter.format("%%%02X", (int) c);// TODO
           }
        }
        return outBuf != null ? outBuf : in;
     }
docs: Encodes characters in the query or fragment part of the URI.
  
  <p>
  Unfortunately, an incoming URI sometimes has characters disallowed by the spec. HttpClient insists that the
  outgoing proxied request has a valid URI because it uses Java's {@link URI}. To be more forgiving, we must escape
  the problematic characters. See the URI class for the spec.
  
  @param in example: name=value&foo=bar#fragment