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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
// We want to load the home page (after login page)
URL url = new URL("http://xxx.yyy.com/web/index.do");
HttpURLConnection connection = null;
String JSESSIONID = null;
try {
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// flush the output
flushOutput(connection.getInputStream(), false);
// extract the JSESSIONID from the "Set-Cookie" header
String cookies = connection.getHeaderField("Set-Cookie");
String[] partsCookies = cookies.split(";");
for (String part : partsCookies) {
if (part.contains("JSESSIONID")) {
String[] subparts = part.split("=");
JSESSIONID = subparts[1];
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
// Post Realm form
String urlJCheck = "http://xxx.yyy.com/web/j_security_check";
URL checkUrl = new URL(urlJCheck);
try {
// open connection
connection = (HttpURLConnection) checkUrl.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setUseCaches(false);
// add headers
Map<String, String> h = new LinkedHashMap<String, String>();
h.put("REFERER", url.toExternalForm());
// I have tried with or without the "; Path=/web" part
h.put("Set-Cookie", "JSESSIONID=" + JSESSIONID + "; Path=/web");
// I have tried with or without all the following headers (in comment or not)
h.put("Content-Type", "application/x-www-form-urlencoded");
h.put("Connection", "keep-alive");
h.put("Keep-Alive", "300");
h.put("Host", "monitoring.alaloop.com");
// h.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
// h.put("Accept-Language", "fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3");
// h.put("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
// h.put("Accept-Encoding", "gzip,deflate");
// h.put("User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; fr; rv:1.9.1.5) Gecko/20091109 Ubuntu/9.10 (karmic) Firefox/3.5.5");
for (String key : h.keySet()) {
connection.addRequestProperty(key, h.get(key));
}
// Prepare parameters for Realm authenticate
StringBuilder parameter = new StringBuilder();
parameter.append("j_username").append("=").append(URLEncoder.encode("Administrator", "UTF-8"));
parameter.append("&");
parameter.append("j_password").append("=").append(URLEncoder.encode("xxxxxxxx", "UTF-8"));
// Post parameters
OutputStream os = connection.getOutputStream();
os.write(parameter.toString().getBytes("UTF-8"));
os.flush();
connection.connect();
// flush the output
flushOutput(connection.getInputStream(), true);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection!=null)
connection.disconnect();
} |
Partager