HTTP URL Address Encoding in Java

前端 未结 26 1406
醉酒成梦
醉酒成梦 2020-11-22 01:35

My Java standalone application gets a URL (which points to a file) from the user and I need to hit it and download it. The problem I am facing is that I am not able to encod

26条回答
  •  臣服心动
    2020-11-22 02:08

    Use the following standard Java solution (passes around 100 of the testcases provided by Web Plattform Tests):

    0. Test if URL is already encoded.

    1. Split URL into structural parts. Use java.net.URL for it.

    2. Encode each structural part properly!

    3. Use IDN.toASCII(putDomainNameHere) to Punycode encode the host name!

    4. Use java.net.URI.toASCIIString() to percent-encode, NFC encoded unicode - (better would be NFKC!).

    Find more here: https://stackoverflow.com/a/49796882/1485527

提交回复
热议问题