Search code examples
javaurl-encodingutf-16

Must use UTF-16 Url encoding to submit a search in Java. How can I?


A certain site (which is not under my control) has an internal search engine that uses GET requests that look like: something.com/search?query=%u0001%0101, which I would like to use in my Java code .

To my understanding this is a not so common way (UTF-16) to do Url encoding. I tried using HttpURLConnection with a Url of the above type, but this throws me a java.net.URISyntaxException Malformed escape pair at index X (X being the position of the %u0001).

What can I do? I'm pretty new to these url encoding issues, so any advice would be highly appreciated.


Solution

  • The form something.com/search?query=%u0001%0101 violates the URI specification as percentage characters are reserved for percent-encoding. Under this rule, a percentage symbol must be followed by two hexadecimal digits. This is not a valid UTF-16 encoded URI.

    It is not surprising that errors are thrown on these addresses.

    You may have to resort to opening a socket and sending your own malformed client request.

    GET /search?query=%u0001%0101 HTTP/1.1
    Host: something.com