-
Notifications
You must be signed in to change notification settings - Fork 0
/
EspConnection.java
169 lines (147 loc) · 7.44 KB
/
EspConnection.java
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import javax.net.ssl.HttpsURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.List;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class EspConnection {
private final String USER_AGENT = "Mozilla/5.0 Gecko Firefox";
private final String HOST;
private final String BASE_URL;
static final String COOKIES_HEADER = "Set-Cookie";
private String cookies;
public EspConnection(String host) {
HOST = host;
BASE_URL = "https://" + HOST;
}
public EspConnection() {
this("hac.pvsd.org");
}
private static boolean isRedirect(int status) {
boolean redirect = false;
if(status != HttpsURLConnection.HTTP_OK) {
if(status == HttpsURLConnection.HTTP_MOVED_TEMP
|| status == HttpsURLConnection.HTTP_MOVED_PERM
|| status == HttpsURLConnection.HTTP_SEE_OTHER)
redirect = true;
}
return redirect;
}
private static String readHttpInputStream(InputStream is) throws IOException {
BufferedReader in = new BufferedReader(
new InputStreamReader(is, "utf-8"));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
public String logOn(String username, String password) throws IOException {
HttpsURLConnection conn = (HttpsURLConnection)
new URL(BASE_URL + "/HomeAccess/Account/LogOn?ReturnUrl=%2fHomeAccess%2fClasses%2fClasswork").openConnection();
byte[] data = String.format(
"Database=10&LogOnDetails.UserName=%s&LogOnDetails.Password=%s", username, password).getBytes("UTF-8");
//conn.setReadTimeout(5000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.addRequestProperty("Origin", BASE_URL);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.9");
conn.addRequestProperty("Upgrade-Insecure-Requests", "1");
conn.addRequestProperty("User-Agent", USER_AGENT);
conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.addRequestProperty("Content-Length", String.valueOf(data.length));
conn.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
conn.addRequestProperty("Cache-Control", "max-age=0");
conn.addRequestProperty("Referer", BASE_URL + "/HomeAccess/Account/LogOn");
conn.addRequestProperty("Connection", "keep-alive");
DataOutputStream wr = new DataOutputStream (conn.getOutputStream());
wr.write(data);
wr.flush();
wr.close();
Map<String, List<String>> headerFields = conn.getHeaderFields();
List<String> cookiesHeader = headerFields.get(COOKIES_HEADER);
StringBuilder cookiesBuilder = new StringBuilder();
if (cookiesHeader != null) {
for (String cookie : cookiesHeader) {
if(!cookie.contains("=;"))
cookiesBuilder.append(cookie.split(";")[0] + "; ");
}
}
cookies = cookiesBuilder.toString();
cookies = cookies.substring(0, cookies.length() - 2);
if(isRedirect(conn.getResponseCode())) {
String newUrl = conn.getHeaderField("Location");
conn = (HttpsURLConnection) new URL(BASE_URL + newUrl).openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setInstanceFollowRedirects(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Host", HOST);
conn.setRequestProperty("Cookie", cookies);
conn.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.9");
conn.addRequestProperty("Upgrade-Insecure-Requests", "1");
conn.addRequestProperty("User-Agent", USER_AGENT);
conn.addRequestProperty("Referer", BASE_URL + "/HomeAccess/Account/LogOn?ReturnUrl=%2fHomeAccess%2fClasses%2fClasswork");
conn.addRequestProperty("Cache-Control", "max-age=0");
conn.addRequestProperty("Connection", "keep-alive");
}
return readHttpInputStream(conn.getInputStream());
}
public String getAssignments() throws IOException {
HttpsURLConnection conn = (HttpsURLConnection)
new URL(BASE_URL + "/HomeAccess/Content/Student/Assignments.aspx").openConnection();
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Host", HOST);
conn.setRequestProperty("Cookie", cookies);
conn.setRequestProperty("Referer", BASE_URL + "/HomeAccess/Classes/Classwork");
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.9");
conn.addRequestProperty("Upgrade-Insecure-Requests", "1");
conn.addRequestProperty("User-Agent", USER_AGENT);
conn.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
conn.addRequestProperty("Connection", "keep-alive");
return readHttpInputStream(conn.getInputStream());
}
public String getTranscript() throws IOException {
HttpsURLConnection conn = (HttpsURLConnection)
new URL(BASE_URL + "/HomeAccess/Content/Student/Transcript.aspx").openConnection();
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Host", HOST);
conn.setRequestProperty("Cookie", cookies);
//conn.setRequestProperty("Referer", BASE_URL + "/HomeAccess/Classes/Classwork");
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.9");
conn.addRequestProperty("Upgrade-Insecure-Requests", "1");
conn.addRequestProperty("User-Agent", USER_AGENT);
conn.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
conn.addRequestProperty("Connection", "keep-alive");
return readHttpInputStream(conn.getInputStream());
}
public String getDemographic() throws IOException {
HttpsURLConnection conn = (HttpsURLConnection)
new URL(BASE_URL + "/HomeAccess/Content/Student/Registration.aspx").openConnection();
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Host", HOST);
conn.setRequestProperty("Cookie", cookies);
//conn.setRequestProperty("Referer", BASE_URL + "/HomeAccess/Registration/Demographic");
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.9");
conn.addRequestProperty("Upgrade-Insecure-Requests", "1");
conn.addRequestProperty("User-Agent", USER_AGENT);
conn.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
conn.addRequestProperty("Connection", "keep-alive");
return readHttpInputStream(conn.getInputStream());
}
}