Quantcast
Channel: WinDev Forum
Viewing all articles
Browse latest Browse all 1914

[wm21 - android] how to use HttpRequest compression (no replies)

$
0
0
Hi,

First make sure your webservice compression is enable.

on NodeJs

const compress = require('compression');
app.use(compress());


on WM21
add "Accept-Encoding:gzip" to http header

HTTPRequest(wsHost,"","Accept-Encoding:gzip", M_Req ,"application/json; charset=utf-8",wsUser,wsPass)

Notes:
for NodeJS, only data above 1024 will be compress. whether the data is compress or not will can know from "Content-Encoding: gzip" in header of response


m_Header = HTTPGetResult(httpHeader)
M_Res = HTTPGetResult(httpResult)



IF Position(m_Header,"Content-Encoding:") > 0
M_Res = UnGzip(Crypt(M_Res,"",cryptNone))
END

Notes , whey must encode to base64 first ?

in order to uncompress we must pass "byte array" , the problem is that WM21 don't support byte array type but HTTPGetResult() return buffer . this is why we encode it to base64 which will return string .


///////////////////////////////////////////////////UnGZIP////////////////////////////////////////////////////////////////
import android.util.Base64;
import java.io.ByteArrayInputStream;
import java.util.zip.GZIPInputStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;


public static String UnGzip(String base64Data)
{
try {
byte[] compressed = android.util.Base64.decode(base64Data, android.util.Base64.DEFAULT);

ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
GZIPInputStream gis = new GZIPInputStream(bis);

BufferedReader br = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line;
while((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
gis.close();
bis.close();
return sb.toString();

} catch (IOException ex) {
return "";

}


}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

compression will save about 25% of data from my test.

Viewing all articles
Browse latest Browse all 1914

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>