我正在获取网页的 html 来获取一些信息,但它总是英文的,我需要西类牙文的,如何使用 java 中的用户代理更改语言,我不太喜欢 javascript或jquery。 获取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;
}
最佳答案
使用 URLConnection 您绝对无法在返回的页面上进行“点击”。不过,您有几个选择,这完全取决于网站的功能:
有一个 Accept-Language您可以在发出的 HTTP 请求上设置 header :
connection.setRequestProperty("Accept-Language", "es_ES");
目标站点可能设置了一些Cookie跟踪您选择的语言,找到它并使用它。您可以找到它,例如在 Chrome 中使用 Developer tools :
在这里您可以看到lang
Cookie 设置为en
。connection.setRequestProperty("Cookie", "lang=en");
语言有时被接受作为请求参数
new URL(lineas + "?lang=en").openConnection();
关于java - 在java中将订单列表更改为西类牙语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36066498/