URLConnection et caractères accentués
Bonjour,
j'essaye de faire un programme Java qui me permet de lire et d'éditer des pages de Wikipédia. Pour éditer une page, je commence par me logguer et pour vérifier que le login a marché, je recherche la chaîne "Identification réussie."
Ca ne marche pas, parce que je reçois en fait "Identification réussie.". Comment on gère les caractères accentués correctement ?
Voici mon code, récupéré depuis http://en.wikipedia.org/wiki/User:MER-C/Wiki.java et modifié:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
public boolean login(String username, String password) throws IOException {
// sanitize
username = URLEncoder.encode(username, "UTF-8");
password = URLEncoder.encode(password, "UTF-8");
// "enable" cookies
String url = wiki.getWikiURL() + "?title=Special:Userlogin";
URLConnection connection = new URL(url).openConnection();
grabCookies(connection);
// find the target
url = wiki.getWikiURL() + "?title=Special:Userlogin&action=submitlogin&type=login";
connection = new URL(url).openConnection();
setCookies(connection);
connection.setDoOutput(true);
PrintWriter out = new PrintWriter(connection.getOutputStream());
// now we send the data
out.print("wpName=");
out.print(username);
out.print("&wpPassword=");
out.print(password);
out.print("&wpRemember=1&wpLoginAttempt=Log+in");
out.close();
// make it stick by grabbing the cookie
grabCookies(connection);
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
} catch (IOException e) {
if (!(connection instanceof HttpURLConnection)) {
throw e;
}
InputStream err = ((HttpURLConnection) connection).getInputStream();
if (err == null) {
throw e;
}
in = new BufferedReader(new InputStreamReader(err));
}
in.readLine();
// test for success
String line;
while ((line = in.readLine()) != null) {
System.err.println(line);
if (line.indexOf("Identification réussie.") != -1) {
return true;
}
}
return false;
} |