Right now I'm opening a URL connection with the following methods
private String downloadLoginPage(URL url) throws IOException { Log.d(PRODUCTION_TAG, "===> " + url);
//readJSON(url);
//InputStream inputStream = null;
BufferedReader bufferedReader = null;
//HttpsURLConnection connection = null;
HttpURLConnection connection = null;
String result = null;
String cookies = "";
int code = 0; //added on 12 - 8 - 2018
try {
//The input form passed from the client to the server is in gzip format. //Any other other type will result in an I/0 error
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json; utf-8");
connection.setRequestProperty("Accept", "application/json"); connection.addRequestProperty("Accept-Encoding", "gzip, deflate, br"); connection.addRequestProperty("X-Requested-With", "XMLHttpRequest"); connection.addRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Cache-Control", "no-cache, no-store, must-revalidate");
connection.setDoInput(true);
connection.setDoOutput(true);
//cookies = connection.getHeaderField("Set-Cookie"); //connection.setRequestProperty("Cookie", cookies); //connection.setChunkedStreamingMode(0); //added on 9 - 12 - 2018 //Fconnection.connect();
Log.e(PRODUCTION_TAG, "----------------------------------------"); Log.e(PRODUCTION_TAG, "THE RESPONSE MESSAGE IS: " + connection.getResponseMessage());
Log.e(PRODUCTION_TAG, "THE RESPONSE REQUEST IS: " + connection.getRequestMethod());
Log.e(PRODUCTION_TAG, "THE RESPONSE CONTENT ENCONDING IS: " + connection.getContentEncoding());
Log.e(PRODUCTION_TAG, "THE RESPONSE HEADERS ARE: " + connection.getHeaderFields());
Log.e(PRODUCTION_TAG, "-----------------------------------"); //Log.e(PRODUCTION_TAG, "THE RESPONSE CONTENT: " + connection.getContent().toString());
//int responsecode = connection.getResponseCode();
int responsecode = ((HttpURLConnection) connection).getResponseCode(); Log.e(PRODUCTION_TAG, "THE RESPONSE CODE IS: " + responsecode);
//int responsecode = 200;
if (responsecode != HttpsURLConnection.HTTP_OK) {
Log.e(PRODUCTION_TAG, "POSSIBLE 404? in downloadLogin()");
//connection.disconnect();
//redirect = true;
//return null;
throw new IOException("Http error code" + responsecode); //disabled in an attempt to debug
}
if ( responsecode == HttpsURLConnection.HTTP_MOVED_TEMP || responsecode == HttpsURLConnection.HTTP_MOVED_PERM) {
Log.e(PRODUCTION_TAG, "REDIRECT DETECTED");
//throw new IOException(" Http error code" + responsecode);
//return null;
throw new IOException("Redirect response code" + responsecode); //added on 10 - 2 - 2018
}
bufferedReader = new BufferedReader((new InputStreamReader(connection.getInputStream())));
InputStream inputStream = connection.getInputStream(); //decodeStream(inputStream);
if (bufferedReader != null) {
//result = readStream(bufferedReader, amGzip, 64000); //Lame way to emulate ping.
result = readStream(inputStream, amGzip, 64000); //Lame way to emulate ping. }
}
finally {
if (bufferedReader != null) {
bufferedReader.close();
}
if (connection != null) {
//connection.disconnect(); //why no else if??
}
}
//OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
//out.write(result);
//out.close();
return result;
}
private String readStream(InputStream inputStreamReader, boolean amGzip, int offset) throws IOException{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
String result = ""; //change from empty to null??! 10 - 4 - 2018
int maxLength = offset; //don't ask
byte [] buffer = new byte[maxLength];
int numChars = 0;
int readSize = 0;
int length = 0;
//START REVISED CODE
//Possibly reading too large data stream. Hence why the insufficient resource error?
/*String result = "";
byte buffer[] = new byte[2048];
int numberOfChars = 0;
try {
new BufferedReader(new InputStreamReader(new GZIPInputStream(stream))); while ((numberOfChars = stream.read()) > 0) {
String app = new String(buffer, 0, numberOfChars);
result = result + app;
}
*/
try {
//new BufferedReader(new InputStreamReader(new GZIPInputStream(inputStreamReader)));
InputStream inputStream = new GZIPInputStream(inputStreamReader);
while((length = inputStream.read(buffer)) > 0) { byteArrayOutputStream.write(buffer, 0, length);
}
Log.e(PRODUCTION_TAG, "THE DECODED STREAM IS: " + byteArrayOutputStream);
} catch (Exception e) {
Log.e(PRODUCTION_TAG, "CAN'T DECODE STREAM BECAUSE: " + e.getMessage()); e.printStackTrace();
}
if (numChars != -1 ) {
//Log.e(PRODUCTION_TAG, "INITIAL HTTP CONNECTION? " + numChars);
numChars = Math.min(numChars, maxLength);
result = new String(buffer, 0, numChars);
}
return result;
}
But instead of getting JSON, I get the viewstate..."#3067db" /><link rel="manifest" href="Content/Common/images/manifest.webmanifest" /><meta name="theme-color" content="#ffffff" /><meta name="msapplication-square70x70logo" content="Content/Common/images/mstile-70x70.png" /><meta name="msapplication-
<!DOCTYPE html>
<html>
<head><meta charset="utf-8" /><meta http-equiv="X-UA-Compatible" content="IE=10; IE=9; IE=8; IE=EDGE" /><meta name="viewport" content="width=device-width, initial-scale=1" /><title>
Dayforce
</title><link rel="shortcut icon" href="Content/Common/images/favicon.ico" /><link rel="apple-touch-icon" sizes="180x180" href="Content/Common/images/apple-touch-icon.png" /><link rel="mask-icon" href="Content/Common/images/safari-pinned-tab.svg" color=
<script type="text/javascript" src="https://static.dayforcehcm.com/libraries/jquery/3.4.1/jquery.min.js"></script><script type="text/javascript" src="/MyDayforce/Scripts/Libs/bootstrap-modal.js"></script></head>YrjwJfqih7mnDds8OYNPmibY7EpW1trUmMiMC3fRXGqcFGgLE4YTiSnNKKL3G5LZHENhG1WzGwt5o0004xSXt23kXSsi6GFmwDB9YLahFQUJWtChj572oHwGabpPAkXETEgsKALwk+kN1xfx3FrrRdEZRhdEapfmDToOpFDACL/BVqEvKABrnhtlPBe1+pIAtjDpcAMdvpKuQ2FXicZ+WOiMqz9L33z9iw+s4+BVtGZMqotxTHRsVKpK+
<body>
<form method="post" action="./MyDayforce.aspx" id="duo_form">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="gZ7CPJpFXjAidNB8ve3X4nYDdg+INMF6dBm3fP2OywHxQjNdEuOuevEDlCsT1h2BuzPPsfy+8mkBACWcn9/zRaSIKBUcGJRFcdxTsokGTj+qTAwBBXGwZFyEaq12v18m9Wq0YGoZGZFInRKuVQsGz+qauv15zx57ZAbDuVEejTKzVCRjO+
THE RESULT URL IS: https://usr57.dayforcehcm.com/MyDayforce/u/I3Q0QXXxm0q45z0RvYNdJA/HR/Employee/GetEmployeeBalanceDisplay
So the question is, how do I get the JSON data instead of the viewstate?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 286 |
Nodes: | 16 (3 / 13) |
Uptime: | 78:39:06 |
Calls: | 6,491 |
Calls today: | 2 |
Files: | 12,096 |
Messages: | 5,276,468 |