From f664b043556edda02e7bb16350d76b5964766f3f Mon Sep 17 00:00:00 2001 From: Ojas Ahuja Date: Thu, 17 Aug 2017 22:44:02 -0500 Subject: [PATCH] fix JSESSIONID cookie --- app/build.gradle | 2 +- .../scraper/TEAMSGradeRetriever.java | 48 ++++++++++++++----- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index cbcbc6c..51c77e2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,7 +7,7 @@ dependencies { } android { compileSdkVersion 21 - buildToolsVersion "21.0.1" + buildToolsVersion '25.0.0' defaultConfig { minSdkVersion 9 diff --git a/app/src/main/java/com/manateams/scraper/TEAMSGradeRetriever.java b/app/src/main/java/com/manateams/scraper/TEAMSGradeRetriever.java index 7af5c0d..b630e26 100644 --- a/app/src/main/java/com/manateams/scraper/TEAMSGradeRetriever.java +++ b/app/src/main/java/com/manateams/scraper/TEAMSGradeRetriever.java @@ -33,7 +33,6 @@ import javax.net.ssl.SSLSocketFactory; - public class TEAMSGradeRetriever { @@ -56,7 +55,7 @@ public TEAMSUserType getUserType(final String username) { public String getNewCookie(final String username, final String password, final TEAMSUserType userType) { try { final String cStoneCookie = getAISDCookie(username, password); - final String TEAMSCookie = getTEAMSCookie(cStoneCookie, userType); + final String TEAMSCookie = getTEAMSCookie(username, password, userType); return cStoneCookie + ';' + TEAMSCookie; } catch (IOException e) { e.printStackTrace(); @@ -135,15 +134,19 @@ public String postTEAMSLogin(final String username, final String password, final } private String getAISDCookie(final String username, final String password) throws IOException { - final String rawQuery = "cn=" + username + "&[password]=" + password; - final String query = URLEncoder.encode(rawQuery, "UTF-8"); + final String rawQuery = "[Client.Hardware]=&[User.ViewportSize]=1920x954&cn=" + username + "&[password]=" + password; final String[] headers = new String[]{ - "User-Agent: QHAC", - "Accept: */*" + "Origin: https://my.austinisd.org", + "Upgrade-Insecure-Requests: 1", + "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0", + "Accept: */*", + "Referer: https://my.austinisd.org/LoginPolicy.jsp", + "Accept-Encoding: gzip, deflate, br", + "Accept-Language: en-US,en;q=0.8" }; - final String response = doRawPOSTRequest("my.austinisd.org", "/WebNetworkAuth/", headers, query); + final String response = doRawPOSTRequest("my.austinisd.org", "/WebNetworkAuth/", headers, rawQuery); for (final String line : response.split("\n")) { if (line.startsWith("Set-Cookie: CStoneSessionID=")) { @@ -154,14 +157,17 @@ private String getAISDCookie(final String username, final String password) throw return null; } - private String getTEAMSCookie(final String AISDCookie, final TEAMSUserType userType) throws IOException { + private String getTEAMSCookie(final String username, final String password, final TEAMSUserType userType) throws IOException { + final String[] headers = new String[]{ - "Cookie: " + AISDCookie, + "Upgrade-Insecure-Requests: 1", + "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0", "Accept: */*", - "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36" + "Accept-Encoding: gzip, deflate, br", + "Accept-Language: en-US,en;q=0.8" }; - final String response = doRawPOSTRequest(userType.teamsHost(), "/selfserve/EntryPointSignOnAction.do?parent=" + userType.isParent(), headers, ""); + final String response = doGETRequest("https://grades.austinisd.org/selfserve/EntryPointSignOnAction.do?parent=false"); for (final String line : response.split("\n")) { if (line.startsWith("Set-Cookie: JSESSIONID=")) { @@ -194,6 +200,26 @@ private String doPOSTRequest(final String url, final HashMap hea return responseString; } + private String doGETRequest(final String url) { + final OkHttpClient client = new OkHttpClient(); + + Request request = new Request.Builder() + .url(url) + .header("User-Agent", "OkHttp Headers.java") + .get() + .build(); + + String responseString = null; + try { + Response response = client.newCall(request).execute(); + if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); + responseString = response.headers().toString(); + } catch (IOException e) { + e.printStackTrace(); + } + return responseString; + } + private String doRawPOSTRequest(final String host, final String path, final String[] headers, final String postData) throws IOException { final Socket socket = SSLSocketFactory.getDefault().createSocket(host, 443);