1 |
torben |
1395 |
package dk.thoerup.telmorewidget; |
2 |
|
|
|
3 |
|
|
import java.io.IOException; |
4 |
|
|
import java.util.HashMap; |
5 |
|
|
import java.util.Map; |
6 |
|
|
|
7 |
|
|
import org.jsoup.Connection; |
8 |
|
|
import org.jsoup.Connection.Method; |
9 |
|
|
import org.jsoup.Connection.Response; |
10 |
|
|
import org.jsoup.helper.HttpConnection; |
11 |
|
|
import org.jsoup.nodes.Document; |
12 |
|
|
import org.jsoup.nodes.Element; |
13 |
|
|
import org.jsoup.select.Elements; |
14 |
|
|
|
15 |
|
|
public class TelmoreFetcher { |
16 |
|
|
|
17 |
|
|
public static class Result { |
18 |
|
|
public String saldo; |
19 |
|
|
public String internetUsage; |
20 |
|
|
} |
21 |
|
|
|
22 |
|
|
|
23 |
|
|
public Result fetch(String username, String password) throws IOException { |
24 |
|
|
|
25 |
|
|
Map<String,String> params = new HashMap<String,String>(); |
26 |
|
|
params.put("j_username", username); |
27 |
|
|
params.put("j_password", password); |
28 |
|
|
|
29 |
|
|
String url = "https://www.telmore.dk/t2/j_security_check"; |
30 |
|
|
|
31 |
|
|
Connection con = HttpConnection.connect(url); |
32 |
|
|
con.data(params); |
33 |
|
|
con.followRedirects(false); |
34 |
|
|
con.method( Method.POST ); |
35 |
|
|
Response resp = con.execute(); |
36 |
|
|
|
37 |
|
|
//System.out.println("" + resp.statusCode() + " " + resp.statusMessage() ); |
38 |
|
|
|
39 |
|
|
String JSESSIONID = resp.cookie("JSESSIONID"); |
40 |
|
|
String location = resp.header( "Location" ); |
41 |
|
|
|
42 |
|
|
/////////////////////////////////////////////////////////////////////////////////// |
43 |
|
|
|
44 |
|
|
resp = HttpConnection.connect(location) |
45 |
|
|
.followRedirects(false) |
46 |
|
|
.cookie("JSESSIONID", JSESSIONID) |
47 |
|
|
.method( Method.GET ) |
48 |
|
|
.execute(); |
49 |
|
|
|
50 |
|
|
//System.out.println("" + resp.statusCode() + " " + resp.statusMessage() ); |
51 |
|
|
location = resp.header( "Location" ); |
52 |
|
|
|
53 |
|
|
/////////////////////////////////////////////////////////////////////////////////// |
54 |
|
|
|
55 |
|
|
resp = HttpConnection.connect(location) |
56 |
|
|
.followRedirects(false) |
57 |
|
|
.cookie("JSESSIONID", JSESSIONID) |
58 |
|
|
.method( Method.GET ) |
59 |
|
|
.execute(); |
60 |
|
|
|
61 |
|
|
//System.out.println("" + resp.statusCode() + " " + resp.statusMessage() ); |
62 |
|
|
Document doc = resp.parse(); |
63 |
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
|
|
String iframeTarget = doc.getElementById("remoteIframePage").attr("abs:src"); |
68 |
|
|
|
69 |
|
|
|
70 |
|
|
/////////////////////////////////////////////////////////////////////////////////// |
71 |
|
|
|
72 |
|
|
|
73 |
|
|
resp = HttpConnection.connect(iframeTarget) |
74 |
|
|
.referrer(location) |
75 |
|
|
.cookie("JSESSIONID", JSESSIONID) |
76 |
|
|
.cookie("SS_X_JSESSIONID", resp.cookie("SS_X_JSESSIONID")) |
77 |
|
|
.followRedirects(false) |
78 |
|
|
.method( Method.GET ) |
79 |
|
|
.execute(); |
80 |
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
|
|
//System.out.println("" + resp.statusCode() + " " + resp.statusMessage() ); |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
doc = resp.parse(); |
89 |
|
|
|
90 |
|
|
Element iframeBody = doc.getElementById("iframeBody"); |
91 |
|
|
|
92 |
|
|
Element saldo = iframeBody.getElementsByClass("saldo").first(); |
93 |
|
|
|
94 |
|
|
//System.out.println("Saldo:" + saldo.text() ); |
95 |
|
|
|
96 |
torben |
1519 |
//TODO: der må være nogle bedre selectors end style |
97 |
torben |
1395 |
Elements internet = iframeBody.getElementsByAttributeValue("style", "padding-top: 1px;"); |
98 |
torben |
1519 |
Element internetMax = iframeBody.getElementsByAttributeValue("style", "width:36px;").get(1); |
99 |
torben |
1395 |
|
100 |
|
|
//System.out.println( "internet count: " + internet.size() ); |
101 |
|
|
//System.out.println( "internet: " + internet.first().text() ); |
102 |
|
|
|
103 |
|
|
Result res = new Result(); |
104 |
torben |
1519 |
res.saldo = saldo.text() + " kr."; |
105 |
|
|
res.internetUsage = internet.first().text() + " of " + internetMax.text() ; |
106 |
torben |
1395 |
|
107 |
|
|
return res; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
/* |
111 |
|
|
void dumpHeaders(Response resp) { |
112 |
|
|
dumpStringmap( resp.headers() ); |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
void dumpCookies(Response resp) { |
116 |
|
|
dumpStringmap(resp.cookies()); |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
void dumpStringmap(Map<String,String> map) { |
120 |
|
|
for (String key : map.keySet() ) { |
121 |
|
|
String val = map.get(key); |
122 |
|
|
System.out.println(">>" + key + ": " + val ); |
123 |
|
|
} |
124 |
|
|
}*/ |
125 |
|
|
} |