Java – how to correctly encode this URL

I tried to get this URL using jsoup

http://betatruebaonline.com/img/parte/330/CIGUE ÑAL.JPG

Even with coding, I have exceptions I don't understand why the code is wrong It's back

But right

How can I solve this problem? thank you.

private static void GetUrl()
{
    try
    {
        String url = "http://betatruebaonline.com/img/parte/330/";
        String encoded = URLEncoder.encode("CIGUEÑAL.JPG","UTF-8");
        Response img = Jsoup
                            .connect(url + encoded)
                            .ignoreContentType(true)
                            .execute();

        System.out.println(url);
        System.out.println("PASSED");
    }
    catch(Exception e)
    {
        System.out.println("Error getting url");
        System.out.println(e.getMessage());
    }
}

Solution

The encoding is correct. The problem here is that the compound Unicode pre combined character "Ñ" can be displayed in two ways. They look the same but are really different

precomposed unicode: Ñ           -> %C3%91
composite unicode: N and ~       -> N%CC%83

I emphasize that both are correct, depending on the type of Unicode you want:

String normalize = Normalizer.normalize("Ñ",Normalizer.Form.NFD);
System.out.println(URLEncoder.encode("Ñ","UTF-8")); //%C3%91
System.out.println(URLEncoder.encode(normalize,"UTF-8")); //N%CC%83
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>