How to determine if a String contains invalid encoded characters

前端 未结 10 1295
眼角桃花
眼角桃花 2020-12-02 11:38

Usage scenario

We have implemented a webservice that our web frontend developers use (via a php api) internally to display product data. On the webs

10条回答
  •  北海茫月
    2020-12-02 11:53

    URLDecoder will decode to a given encoding. This should flag errors appropriately. However the documentation states:

    There are two possible ways in which this decoder could deal with illegal strings. It could either leave illegal characters alone or it could throw an IllegalArgumentException. Which approach the decoder takes is left to the implementation.

    So you should probably try it. Note also (from the decode() method documentation):

    The World Wide Web Consortium Recommendation states that UTF-8 should be used. Not doing so may introduce incompatibilites

    so there's something else to think about!

    EDIT: Apache Commons URLDecode claims to throw appropriate exceptions for bad encodings.

提交回复
热议问题