Diese Seite mit anderen teilen ...

Informationen zum Thema:
Forum:
WinDev Forum
Beiträge im Thema:
4
Erster Beitrag:
vor 6 Monaten, 3 Wochen
Letzter Beitrag:
vor 6 Monaten, 3 Wochen
Beteiligte Autoren:
ccc2, Ruben Sanchez Peña

[wm21 - android] how to use HttpRequest compression

Startbeitrag von ccc2 am 03.05.2017 09:38

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.

Antworten:

Hi ,

the above post work for text

For Binary data .



//// create a procedure with buffer parameter and return buffer ////

PROCEDURE UnGzip(LOCAL p_buffer is Buffer)

m_base64 is string = Crypt(p_buffer,"",cryptNone)

m_base64 = J_UnGzip(m_base64)

Uncrypt(m_base64,"",cryptNone,encodeBASE64)




//////////////// J_UnGzip //////////////////////////////////////////////////////////////


import android.util.Base64;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.GZIPInputStream;



public static String J_UnGzip(String base64Data)
{

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

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

byte[] buffer = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();


int len;
while ((len = gis.read(buffer)) > 0) {
bos.write(buffer, 0, len);
}

gis.close();
bos.flush();
bos.close();
bis.close();

return android.util.Base64.encodeToString(bos.toByteArray(), Base64.DEFAULT);

} catch (IOException ex) {

return "";
}


}

von ccc2 - am 04.05.2017 05:26
Woow, very impresive. Thank You.

Rubén Sánchez

von Ruben Sanchez Peña - am 04.05.2017 08:08
Hi,

if you get java.io.EOFException exception , add try catch and ignore the exception

/////////////////////////////////////////////////////////////////////////////////////////////////
import java.io.EOFException;

try {

int len;
while ((len = gis.read(buffer)) > 0) {
bos.write(buffer, 0, len);
}

} catch(EOFException ex_EOF) {
//EOF


}

////////////////////////////////////////////////////////////////////////////////////////////////
The problem is with WM base64 encode which sometime causing missing termination. from WM20 to WM21 this problem persist and I don't know why or solution.

von ccc2 - am 04.05.2017 12:51
Zur Information:
MySnip.de hat keinen Einfluss auf die Inhalte der Beiträge. Bitte kontaktieren Sie den Administrator des Forums bei Problemen oder Löschforderungen über die Kontaktseite.
Falls die Kontaktaufnahme mit dem Administrator des Forums fehlschlägt, kontaktieren Sie uns bitte über die in unserem Impressum angegebenen Daten.