• How do I get JSON data instead of viewstate?

    From C.D. Altenburg, Ph.D,@21:1/5 to All on Mon Jul 6 07:27:02 2020
    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...

    <!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="#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-square150x150logo" content="Content/Common/images/mstile-150x150.png" /><meta name="msapplication-wide310x150logo" content="Content/Common/images/mstile-310x150.png" /><meta name="msapplication-square310x310logo" content="Content/Common/
    images/mstile-310x310.png" /><meta name="msapplication-TileColor" content="#ffffff" /><link id="styles" rel="stylesheet" href="/MyDayforce/Content/Common/styles/rebrandedCss?v=D7kcseGKVfP49nJNTdXABaiA3me99jgF-8a7GIDzPiw1" />
    <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>
    <body>
    <form method="post" action="./MyDayforce.aspx" id="duo_form">
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="gZ7CPJpFXjAidNB8ve3X4nYDdg+INMF6dBm3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2vJ17mflOCWSep7hB3skI63H9761lzfnhInbPcEio76lH//cRkNWat9x3OLZ/
    us91VC6VSt1I+YfakILN0u2ivs6V4TS2zvdzcEAptEOKIWl8M87oQLZHLysxuoUx091tkSCI420JtApdWBMYEDBq+nhz4tIChXPyjGyKFIjtQ8TWvQq8N31ZxVK7xeCvTaSyr6Mai7GIofbA5h25/dJ6EA6LEbzcNa4edhWI/EIhUTTmkuoZhqLTFYpgjgIAW/+gaJJueaWFfXtZrf0wYQsPZ5fgdAnBx8Oz/cMjMD
    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?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Joerg Meier@21:1/5 to All on Mon Jul 6 23:39:45 2020
    On Mon, 6 Jul 2020 07:27:02 -0700 (PDT), C.D. Altenburg, Ph.D, wrote:

    Right now I'm opening a URL connection with the following methods

    [...]

    Okay, there is no nice way to say this: that code is terrible and would literally serve as an example of common beginner mistakes. I don't want to
    be mean about it, but the sooner your throw it away and just use a proper library or modern code the better.

    Personally, I would go with Retrofit 2 which uses OkHttp and Jackson under
    the hood (by default), but there are many great solutions for this common
    use case these days, and none of them involve writing low level code like
    this.

    So the question is, how do I get the JSON data instead of the viewstate?

    Sadly, I can't even help you with your actual problem: because that's on
    the server side. We have no idea how to get a random website to serve JSON
    if it isn't. :(

    Liebe Gruesse,
    Joerg

    --
    Ich lese meine Emails nicht, replies to Email bleiben also leider
    ungelesen.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From C.D. Altenburg, Ph.D,@21:1/5 to All on Mon Jul 6 17:44:18 2020
    Someone had suggested Retrofit before. I need to figure out how to install it on Android. This could be a while since I just recently learned how to use things like git and React from the command line. My excuse is that I'm a Windows point and click type
    of person.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From C.D. Altenburg, Ph.D,@21:1/5 to All on Mon Jul 6 17:20:17 2020
    Fair enough

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)