简体   繁体   中英

Change the order list to spanish in java

I'm getting the web page's html to get some information, but it always is in English, I need it in spanish, how can I change the language using a user-agent in java, I'm not really into javascript or jquery. Code to get the html

private String conexion(String lineas) {
    System.setProperty("java.net.useSystemProxies", "true");
    String content = null;
    URLConnection connection = null;
    try {
        connection = new URL(lineas).openConnection();
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
        Scanner scanner = new Scanner(connection.getInputStream());
        scanner.useDelimiter("\\Z");
        content = scanner.next();
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "¡¡Ese link no Existe!!", "Error", JOptionPane.ERROR_MESSAGE);
    }
    return content;
}

In the page, to change the language is with a list. 码

Using URLConnection you definitely won't be able to make 'clicks' on the returned page. However you have a few options, it all depends on web site capabilities :

  1. There's an Accept-Language Header you can set on the HTTP request you make:

     connection.setRequestProperty("Accept-Language", "es_ES"); 
  2. The target site probably set some Cookie to keep track of the language you've chosen, find it and use it. You can find it eg in Chrome with Developer tools : 在此处输入图片说明
    Here you can see the lang Cookie set to en .

     connection.setRequestProperty("Cookie", "lang=en"); 
  3. The language is sometimes accepted as a request parameter

     new URL(lineas + "?lang=en").openConnection(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM