Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #39396

cross post. How do I get the http POST body?

Newsgroups comp.lang.java.programmer
Date 2020-03-16 07:47 -0700
Message-ID <16110cea-bf84-4823-ae22-adf692a1f499@googlegroups.com> (permalink)
Subject cross post. How do I get the http POST body?
From "C.D. Altenburg, Ph.D," <cdalten@gmail.com>

Show all headers | View raw


I'm not going to post all 8000 lines to the engineering wonder.... 

The question is how do I get the post body in Java. Ie, the http body has the following format 

HTTP/1.1 200 OK 
Cache-Control: no-cache, no-store, must-revalidate 
Pragma: no-cache 
Content-Type: application/json 
Expires: -1 
Vary: Accept-Encoding 
Server: Microsoft-IIS/10.0 
x-frame-options: DENY 
Date: Mon, 16 Mar 2020 14:30:03 GMT 
Content-Length: 1034 
Strict-Transport-Security: max-age=31536000 
X-CDN: Incapsula 
X-Iinfo: 9-31663561-31663518 PNNy RT(1584369003228 523) q(0 0 0 -1) r(3 3) U6 

{"SecuredPropertiesKey":"00000000-0000-0000-0000-000000000000","Success":true,"ErrorMessage":null,"Errors":[],"Warnings":[],"Informationals":[],"Result":{"EmployeeBalanceErrors":null,"EmployeeId":9414,"ExpiringTransactions":[],"Header":["Type","Accrued","Approved","Pending","Remaining","Exceeded","Unit"],"Totals":[{"BalanceId":3,"BalanceName":"PTO","UnitName":"Hours","BalancePeriodEnd":null,"BalancePeriodStart":"2020-01-01T00:00:00","Columns":[{"Header":"Type","Display":"PTO","Value":"PTO","ColumnType":1},{"Header":"Accrued","Display":"17.43737","Value":17.43737,"ColumnType":7},{"Header":"Approved","Display":"-3.50000","Value":-3.50000,"ColumnType":7},{"Header":"Pending","Display":"0.00000","Value":0.0,"ColumnType":7},{"Header":"Remaining","Display":"13.93737","Value":13.93737,"ColumnType":7},{"Header":"Exceeded","Display":"0.00000","Value":0.0,"ColumnType":7},{"Header":"Unit","Display":"Hours","Value":"Hours","ColumnType":6}],"IsGrantBased":false,"IsAccessRestricted":false,"SortOrder":-2147483648}],"Transactions":[]}} 

I need all the data below X-Iinfo. 

Here is my really long and ugly initial attempt in java... 

private String downloadLoginPage(URL url) throws IOException { 
        Log.d(PRODUCTION_TAG, "===> " + url); 
        readJSON(url); 
        InputStream inputStream = null; 
        //HttpsURLConnection connection = null; 
        HttpURLConnection connection = null; 
        String result = null; 
        boolean redirect = false; //added on 9 - 25 - 2018 
        int code = 0; //added on 12 - 8 - 2018 
        try { 
            //connection = (HttpsURLConnection) url.openConnection(); 
            connection = (HttpURLConnection) url.openConnection(); 
            //HttpsURLConnection.setFollowRedirects(false); 
            //connection.setInstanceFollowRedirects(true); 
            //connection.setReadTimeout(9000); //3000ms 
            //connection.setConnectTimeout(9000); //3000ms 
            //connection.setRequestMethod("GET"); 

            if (CookieManager.getInstance().getCookie(connection.getURL().toString()) != null) { 
                //String arr[] = CookieManager.getInstance().getCookie(connection.getURL().toString()).split(";"); 
                //connection.setRequestProperty("Cookie", arr[1]); 
                //Log.i(PRODUCTION_TAG, "The client side cookie is: " + arr[1]); 
            } 

            connection.setDoInput(true); 
            connection.setRequestMethod("POST"); 
            connection.setRequestProperty("Content-Type", 
                    "application/x-www-form-urlencoded"); 

            //connection.setUseCaches(true); 
//connection.addRequestProperty("Accept", "text/*"); 
            //connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 
            //connection.addRequestProperty("Accept", "application/json"); 
            //connection.setRequestProperty("Accept", "text/html, text/*"); 
            //connection.addRequestProperty("Cache-Control", "no-cache"); 
            //if (url.toString().contains("GetEmployeeBalanceDisplay")) { 
                connection.setRequestProperty("Cache-Control", "no-cache, no-store, must-revalidate"); 
            //} 

            //connection.setChunkedStreamingMode(0); //added on 9 - 12 - 2018 
            connection.connect(); 
            Log.e(PRODUCTION_TAG, "THE RESPONSE HEADERS ARE: " + connection.getHeaderFields()); 

            //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 
            } 

            inputStream = connection.getInputStream(); 
            Log.e(PRODUCTION_TAG, "THE RESPONSE MESSAGE IS: " + 
                    readStream((InputStream) connection.getContent(), 5000)); 



            if (inputStream != null) { 
                result = readStream(inputStream, 64000); //Lame way to emulate ping. 
            } 
        } 

        finally { 
            if (inputStream != null) { 
                inputStream.close(); 
            } 
            if (connection != null) { 
                //connection.disconnect(); //why no else if?? 
            } 
        } 
        return result; 
    } 

//added on 8 - 16 - 2018. 
    private String readStream(InputStream stream, int offset) throws IOException{ 
        InputStreamReader inputStreamReader = new InputStreamReader(stream, "UTF-8"); 

        String result = null; //change from empty to null??! 10 - 4 - 2018 
        int maxLength = offset; //don't ask 
        //int maxLength = 100; //modified on 10 - 7 - 2018 
        char [] buffer = new char[maxLength]; 
        int numChars = 0; 
        int readSize = 0; 

        /START REVISED CODE 
        //Possibly reading too large data stream. Hence why the insufficient resource error? 
        //Log.d(PRODUCTION_TAG, "STARTED TO READ STREAM"); //added on 10 - 4- 2018 
        while (numChars < maxLength && readSize != -1) { 
            numChars += readSize;; 
            inputStreamReader.read(); 
            readSize = inputStreamReader.read(buffer, numChars, buffer.length - numChars); 
        } 

        if (numChars != -1 ) { 
            //Log.e(PRODUCTION_TAG, "INITIAL HTTP CONNECTION? " + numChars); 
            numChars = Math.min(numChars, maxLength); 
            result = new String(buffer, 0, numChars); 
        } 

        //Log.e(PRODUCTION_TAG, "===> " + result); 
        return result; 
        //return output; //close stream?? 
    } 

But all this yields is what appears to be the input form? 

03-16 07:21:15.415 5938-5970/com.example.cd.workreminder E/LG_WORK_WEB:: THE RESPONSE MESSAGE IS: 
    <!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=nwLz1BN8ZUfRwzR_Cq1k8qgVoiBwHNkZkQYcDHpSaok1" /> 
    <script type="text/javascript" src="https://static.dayforcehcm.comlibraries/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="7x5HGGKLqVDLU6TCIkiSRMEWoO4J2qwN+193zs+43Q5HQ9Hl4tu/iXcDcqTV6T3Fdr9qnukmnuTXhqk2Rg+4+QB5aO4t9cLysPlDGFju6JyZMD8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pajrBOObPY5nEFq2pPeRzekFmWVXktP0z1Pc8l/osGrGTWPIcddw/Ktwjn+0ZOa4MT/F98VKJtOUbtMwXha2AdryqMjqH8jbbt873C/RHmbLxJO/8pshLRN99R4pbQ7TnDaOB1VG+xB07DSJU6cDH8/RFFG5gnkgUlI3MrMCnaJGsnQbtXiPLoukzCNPiVugEczp2iL7zW7 
03-16 07:23:27.791 5938-5970/com.example.cd.workreminder E/LG_WORK_WEB:: THE RESPONSE MESSAGE IS: 
    <!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>

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

cross post. How do I get the http POST body? "C.D. Altenburg, Ph.D," <cdalten@gmail.com> - 2020-03-16 07:47 -0700
  Re: cross post. How do I get the http POST body? Arne Vajhøj <arne@vajhoej.dk> - 2020-03-16 11:13 -0400
    Re: cross post. How do I get the http POST body? "C.D. Altenburg, Ph.D," <cdalten@gmail.com> - 2020-03-17 04:52 -0700
      Re: cross post. How do I get the http POST body? Arne Vajhøj <arne@vajhoej.dk> - 2020-03-17 10:05 -0400
      Re: cross post. How do I get the http POST body? Joerg Meier <joergmmeier@arcor.de> - 2020-03-17 15:11 +0100

csiph-web