UTF-8 for URL, Java

So, I'm trying to clean up a grammar website that gives you verb conjugations, but I'm having trouble accessing pages that require accents, like the page for the "fág" verb.

Here is my current code:

    String url = "http://www.teanglann.ie/en/gram/"+ URLEncoder.encode("fág","UTF-8");
    System.out.println(url);

      

I've tried this both with and without the URLEncoder.encode () method and it just gives me a "?" instead of "á" when working with it and my URL search returns nothing. Basically, I was wondering if there was something similar to Python's 'urllib.parse.quote_plus'. I have tried to find and try many different methods from StackOverflow to no avail. Any help would be greatly appreciated.

Eventually, I'm going to replace the given string with a custom argument. Just use it for testing for now.

Solution . It wasn't Java, but IntelliJ.

+3


source to share


1 answer


Summary from comment

The test code works fine.

import java.io.UnsupportedEncodingException;
import static java.net.URLEncoder.encode;

public class MainApp {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String url = "http://www.teanglann.ie/en/gram/"+ encode("fág", "UTF-8");
        System.out.println(url);
    }
}

      

It is emitted as shown below

http://www.teanglann.ie/en/gram/f%EF%BF%BDg

What will happen to the right page.

Correct steps



  • Make sure the encoding of the source code is correct. (IntelliJ may not be able to guess that this is all correct)
  • Run the program with the appropriate encoding (utf-8 in this case)

(see   What is the standard JVM encoding?  for related discussion)

Edit Wyzard's comment

Works on code randomly (say no spaces). The correct way to get the encoded url is like below ..

 String url = "http://www.teanglann.ie/en/gram/fág";
 System.out.println(new URI(url).toASCIIString());

      

This uses URI.toASCIIString (), which adheres to RFC 2396 , which talks about uniform resource identifiers (URIs): general syntax

+2


source







All Articles