package dk.thoerup.spejdernetscraper; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URLEncoder; import java.util.Collection; import java.util.List; import java.util.Map; import org.jsoup.Connection; import org.jsoup.Connection.Method; import org.jsoup.Connection.Request; import org.jsoup.Connection.Response; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; public class OKMobilSaldo { final String mobil = "71750247"; final String password = "gc93ehcv"; final String authKey = ".ASPXAUTH"; String chromeUserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"; public String hentSaldo() { try { String frontpageUrl = "https://www.ok-mobil.dk/"; String loginUrl = "https://www.ok-mobil.dk/log-paa"; String accountUrl = "https://www.ok-mobil.dk/min-konto"; Response res = Jsoup.connect(frontpageUrl) .userAgent(chromeUserAgent) .execute(); Map cookies = res.cookies(); /* da OK -mobil sender AUTH cookie to gange skal der lidt ekstra til for at finde den rigtige */ Request req = Jsoup .connect( loginUrl ) .userAgent(chromeUserAgent) .referrer(frontpageUrl) .data("phoneNumber", mobil, "password", password ) .method(Method.POST) .cookies(cookies) .request(); HttpURLConnection conn = createConnection(req); conn.connect(); if (req.method() == Connection.Method.POST) writePost(req.data(), conn.getOutputStream()); int status = conn.getResponseCode(); String auth = getAuthHeader(conn.getHeaderFields() ); System.out.println("AUTH: " + auth); cookies.put(authKey, auth); /* slut på OK cookie */ Document accountPage = Jsoup.connect(accountUrl) .cookies(cookies) .userAgent(chromeUserAgent) .get(); Elements infoElems = accountPage.getElementsByClass("cashBalance"); System.out.println(accountPage.text() ); return infoElems.text(); } catch (Exception e) { return "Fejl: " + e.getMessage(); } } // My helper functions String mapToString(Map map) { StringBuilder sb = new StringBuilder(); for (String key : map.keySet()) { sb.append(key).append(": ").append(map.get(key)).append("\n"); } return sb.toString(); } String maplistToString(Map> map) { StringBuilder sb = new StringBuilder(); for (String key : map.keySet()) { List vals = map.get(key); for (String val : vals) { sb.append(key).append(": ").append(val).append("\n"); } } return sb.toString(); } String getAuthHeader(Map> map) { for (String key : map.keySet()) { System.out.println("Key:" + key); if (key == null) //Warning key may be null continue; if (key.equalsIgnoreCase("Set-Cookie")) { List vals = map.get(key); for (String val : vals) { String parts[] = val.split(";"); String baseCookie[] = parts[0].split("="); String cookieName = baseCookie[0]; System.out.println("Cookie:" + cookieName); if (cookieName.equalsIgnoreCase(authKey)) { if (baseCookie.length==2 && baseCookie[1].length() > 10) return baseCookie[1]; } } } } return null; } //Snipped from org.jsoup.helper.HttpConnection.java private static HttpURLConnection createConnection(Connection.Request req) throws IOException { HttpURLConnection conn = (HttpURLConnection) req.url().openConnection(); conn.setRequestMethod(req.method().name()); conn.setInstanceFollowRedirects(false); // don't rely on native redirection support conn.setConnectTimeout(req.timeout()); conn.setReadTimeout(req.timeout()); if (req.method() == Method.POST) conn.setDoOutput(true); if (req.cookies().size() > 0) conn.addRequestProperty("Cookie", getRequestCookieString(req)); for (Map.Entry header : req.headers().entrySet()) { conn.addRequestProperty(header.getKey(), header.getValue()); } return conn; } private static String getRequestCookieString(Connection.Request req) { StringBuilder sb = new StringBuilder(); boolean first = true; for (Map.Entry cookie : req.cookies().entrySet()) { if (!first) sb.append("; "); else first = false; sb.append(cookie.getKey()).append('=').append(cookie.getValue()); // todo: spec says only ascii, no escaping / encoding defined. validate on set? or escape somehow here? } return sb.toString(); } private static void writePost(Collection data, OutputStream outputStream) throws IOException { OutputStreamWriter w = new OutputStreamWriter(outputStream, "UTF-8"); boolean first = true; for (Connection.KeyVal keyVal : data) { if (!first) w.append('&'); else first = false; w.write(URLEncoder.encode(keyVal.key(), "UTF-8")); w.write('='); w.write(URLEncoder.encode(keyVal.value(), "UTF-8")); } w.close(); } }