Java URL encoding of query string parameters

前端 未结 12 1022
清歌不尽
清歌不尽 2020-11-21 05:27

Say I have a URL

http://example.com/query?q=

and I have a query entered by the user such as:

random word £500 bank

相关标签:
12条回答
  • 2020-11-21 05:52

    In android I would use this code:

    Uri myUI = Uri.parse ("http://example.com/query").buildUpon().appendQueryParameter("q","random word A3500 bank 24").build();
    

    Where Uri is a android.net.Uri

    0 讨论(0)
  • 2020-11-21 05:53

    URLEncoder is the way to go. You only need to keep in mind to encode only the individual query string parameter name and/or value, not the entire URL, for sure not the query string parameter separator character & nor the parameter name-value separator character =.

    String q = "random word £500 bank $";
    String url = "https://example.com?q=" + URLEncoder.encode(q, StandardCharsets.UTF_8);
    

    When you're still not on Java 10 or newer, then use StandardCharsets.UTF_8.toString() as charset argument, or when you're still not on Java 7 or newer, then use "UTF-8".


    Note that spaces in query parameters are represented by +, not %20, which is legitimately valid. The %20 is usually to be used to represent spaces in URI itself (the part before the URI-query string separator character ?), not in query string (the part after ?).

    Also note that there are three encode() methods. One without Charset as second argument and another with String as second argument which throws a checked exception. The one without Charset argument is deprecated. Never use it and always specify the Charset argument. The javadoc even explicitly recommends to use the UTF-8 encoding, as mandated by RFC3986 and W3C.

    All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. Then each byte is represented by the 3-character string "%xy", where xy is the two-digit hexadecimal representation of the byte. The recommended encoding scheme to use is UTF-8. However, for compatibility reasons, if an encoding is not specified, then the default encoding of the platform is used.

    See also:

    • What every web developer must know about URL encoding
    0 讨论(0)
  • 2020-11-21 05:54

    I would not use URLEncoder. Besides being incorrectly named (URLEncoder has nothing to do with URLs), inefficient (it uses a StringBuffer instead of Builder and does a couple of other things that are slow) Its also way too easy to screw it up.

    Instead I would use URIBuilder or Spring's org.springframework.web.util.UriUtils.encodeQuery or Commons Apache HttpClient. The reason being you have to escape the query parameters name (ie BalusC's answer q) differently than the parameter value.

    The only downside to the above (that I found out painfully) is that URL's are not a true subset of URI's.

    Sample code:

    import org.apache.http.client.utils.URIBuilder;
    
    URIBuilder ub = new URIBuilder("http://example.com/query");
    ub.addParameter("q", "random word £500 bank \$");
    String url = ub.toString();
    
    // Result: http://example.com/query?q=random+word+%C2%A3500+bank+%24
    

    Since I'm just linking to other answers I marked this as a community wiki. Feel free to edit.

    0 讨论(0)
  • 2020-11-21 05:58

    I found an easy solution to your question. I also wanted to use an encoded URL but nothing helped me.

    http://example.com/query?q=random%20word%20%A3500%20bank%20%24

    to use String example = "random word £500 bank $"; you can you below code.

    String example = "random word £500 bank $";
    String URL = "http://example.com/query?q=" + example.replaceAll(" ","%20");
    
    0 讨论(0)
  • 2020-11-21 06:07

    You need to first create a URI like:

    String urlStr = "http://www.example.com/CEREC® Materials & Accessories/IPS Empress® CAD.pdf"
    URL url= new URL(urlStr);
    URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
    

    Then convert that Uri to ASCII string:

    urlStr=uri.toASCIIString();
    

    Now your url string is completely encoded first we did simple url encoding and then we converted it to ASCII String to make sure no character outside US-ASCII are remaining in string. This is exactly how browsers do.

    0 讨论(0)
  • 2020-11-21 06:07
    1. Use this: URLEncoder.encode(query, StandardCharsets.UTF_8.displayName()); or this:URLEncoder.encode(query, "UTF-8");
    2. You can use the follwing code.

      String encodedUrl1 = UriUtils.encodeQuery(query, "UTF-8");//not change 
      String encodedUrl2 = URLEncoder.encode(query, "UTF-8");//changed
      String encodedUrl3 = URLEncoder.encode(query, StandardCharsets.UTF_8.displayName());//changed
      
      System.out.println("url1 " + encodedUrl1 + "\n" + "url2=" + encodedUrl2 + "\n" + "url3=" + encodedUrl3);
      
    0 讨论(0)
提交回复
热议问题