3 |
import java.util.Map; |
import java.util.Map; |
4 |
import java.util.TreeMap; |
import java.util.TreeMap; |
5 |
|
|
|
import javax.servlet.http.HttpUtils; |
|
|
|
|
6 |
import org.jsoup.Connection.Method; |
import org.jsoup.Connection.Method; |
7 |
import org.jsoup.Connection.Response; |
import org.jsoup.Connection.Response; |
8 |
import org.jsoup.Jsoup; |
import org.jsoup.Jsoup; |
9 |
|
import org.jsoup.nodes.Document; |
10 |
|
import org.jsoup.nodes.Element; |
11 |
|
|
12 |
import dk.thoerup.genericjavautils.HttpUtil; |
import dk.thoerup.genericjavautils.HttpUtil; |
13 |
|
|
15 |
|
|
16 |
@Override |
@Override |
17 |
public void run() { |
public void run() { |
18 |
long start = System.currentTimeMillis(); |
|
19 |
ResultBuffer rb = ResultBuffer.getResultBuffer(); |
ResultBuffer rb = ResultBuffer.getResultBuffer(); |
20 |
try { |
try { |
21 |
|
|
22 |
rb.clear(); |
rb.clear(); |
23 |
rb.addString(start, "Starting"); |
rb.addString("Starting"); |
24 |
|
|
25 |
|
|
26 |
Response res = Jsoup.connect("http://medlemssystem.spejdernet.dk/login.aspx?ReturnUrl=%2f").execute(); |
Response res = Jsoup.connect("http://medlemssystem.spejdernet.dk/login.aspx?ReturnUrl=%2f").execute(); |
27 |
Map<String, String> cookies = res.cookies(); |
Map<String, String> cookies = res.cookies(); |
28 |
|
|
29 |
rb.addString(start, "Got login page"); |
Document loginPage = res.parse(); |
30 |
|
|
31 |
|
Element viewState = loginPage.getElementById("__VIEWSTATE"); |
32 |
|
|
33 |
|
|
34 |
|
rb.addString("Got login page"); |
35 |
|
|
36 |
res = Jsoup |
res = Jsoup |
37 |
.connect("http://medlemssystem.spejdernet.dk/Login.aspx?changeuser=1") |
.connect("http://medlemssystem.spejdernet.dk/Login.aspx?changeuser=1") |
40 |
"ctl00$main$loginForm$_buttonLogin", "Login", |
"ctl00$main$loginForm$_buttonLogin", "Login", |
41 |
"__EVENTTARGET", "", |
"__EVENTTARGET", "", |
42 |
"__EVENTARGUMENT", "", |
"__EVENTARGUMENT", "", |
43 |
"__VIEWSTATE", "/wEPDwUJNjg2MjA4NzU2D2QWAmYPZBYCAgMPZBYGAgEPDxYCHgdWaXNpYmxlaGRkAgIPZBYIAgkPDxYCHwBoZGQCDQ8PFgIfAGhkZAIPDw8WAh8AaGRkAhMPDxYCHwBoZGQCAw9kFgICAQ9kFgRmDw8WAh8AaGRkAgYPDxYCHwBoZGRk8Vg6q/CGyZv+RzwBkdn5NiLJnoGAlWgM6iWe62LJuHM=", |
"__VIEWSTATE", viewState.attr("value"), |
44 |
"ctl00$ctl04$hiddenTab", "" |
"ctl00$ctl04$hiddenTab", "" |
45 |
) |
) |
46 |
.method(Method.POST) |
.method(Method.POST) |
47 |
.cookies(cookies) |
.cookies(cookies) |
48 |
.execute(); |
.execute(); |
49 |
|
|
50 |
rb.addString(start, "Login OK"); |
rb.addString("Login OK"); |
51 |
//This will get you cookies |
//This will get you cookies |
52 |
cookies.putAll( res.cookies() ); |
cookies.putAll( res.cookies() ); |
53 |
//printMap(cookies); |
//printMap(cookies); |
57 |
//And this is the easieste way I've found to remain in session |
//And this is the easieste way I've found to remain in session |
58 |
Response data = Jsoup.connect(dataCsvUrl).cookies(cookies).timeout(10000).execute(); |
Response data = Jsoup.connect(dataCsvUrl).cookies(cookies).timeout(10000).execute(); |
59 |
if ( ! data.contentType().equalsIgnoreCase("text/x-csv; charset=iso-8859-1") ) { |
if ( ! data.contentType().equalsIgnoreCase("text/x-csv; charset=iso-8859-1") ) { |
60 |
rb.addString(start, "Data.csv - has the wrong content type: " + data.contentType()); |
|
61 |
|
rb.addString("Data.csv - has the wrong content type: " + data.contentType()); |
62 |
return; |
return; |
63 |
} |
} |
64 |
String dataCsv = data.body(); |
String dataCsv = data.body(); |
65 |
rb.addString(start, "Got data.csv"); |
rb.addString("Got data.csv"); |
66 |
|
|
67 |
String rollerCsvUrl = "http://medlemssystem.spejdernet.dk/DataExport.aspx?Id=5c0cf638-dc87-413f-91b6-9c7300c70137&listid=e8c5ae9d-5ea7-4a00-bea4-a0ce00ea891e&execute=true"; |
String rollerCsvUrl = "http://medlemssystem.spejdernet.dk/DataExport.aspx?Id=5c0cf638-dc87-413f-91b6-9c7300c70137&listid=e8c5ae9d-5ea7-4a00-bea4-a0ce00ea891e&execute=true"; |
68 |
Response roller = Jsoup.connect(rollerCsvUrl).cookies(cookies).timeout(10000).execute(); |
Response roller = Jsoup.connect(rollerCsvUrl).cookies(cookies).timeout(10000).execute(); |
69 |
if ( ! roller.contentType().equalsIgnoreCase("text/x-csv; charset=iso-8859-1") ) { |
if ( ! roller.contentType().equalsIgnoreCase("text/x-csv; charset=iso-8859-1") ) { |
70 |
rb.addString(start, "Roller.csv - has the wrong content type: " + roller.contentType()); |
rb.addString("Roller.csv - has the wrong content type: " + roller.contentType()); |
71 |
return; |
return; |
72 |
} |
} |
73 |
String rollerCsv = roller.body(); |
String rollerCsv = roller.body(); |
74 |
rb.addString(start, "Got roller.csv"); |
rb.addString("Got roller.csv"); |
75 |
|
|
76 |
Map<String,String> postData = new TreeMap<String,String>(); |
Map<String,String> postData = new TreeMap<String,String>(); |
77 |
postData.put("roller", rollerCsv); |
postData.put("roller", rollerCsv); |
81 |
byte resp[] = HttpUtil.postContent("http://horsensspejder.t-hoerup.dk/data/postdata.php", params, 3000); |
byte resp[] = HttpUtil.postContent("http://horsensspejder.t-hoerup.dk/data/postdata.php", params, 3000); |
82 |
|
|
83 |
String response = new String(resp); |
String response = new String(resp); |
84 |
rb.addString(start, "POST Response: " + response); |
rb.addString("POST Response: " + response); |
85 |
|
|
86 |
rb.addString(start, "Done !"); |
rb.addString("Done !"); |
87 |
|
|
88 |
} catch (Exception e) { |
} catch (Exception e) { |
89 |
rb.addString(start, "Error occurred: " + e.toString() ); |
rb.addString("Error occurred: " + e.toString() ); |
90 |
} |
} |
91 |
} |
} |
92 |
|
|