[英]How to send string compressed with GZIP from Java App to PHP web service
I have this issue with GZIP compression: 我有GZIP压缩这个问题:
I need to send by POST method a huge JSON string, which is too big to be accept like URL (Ex: http://localhost/app/send/ JSON STRING ENCODED BY BASE64 ), than it result in HTTP error 403 我需要通过POST方法发送一个巨大的JSON字符串,这个字符串太大而不能接受像URL(例如: http:// localhost / app / send / JSON STRING ENCODED by BASE64 ),而不是导致HTTP错误403
so, I need to compress my json and I found a way to do it with GZIP compression, which I can decompress with gzdecode() in PHP. 所以,我需要压缩我的json,我找到了一种方法来使用GZIP压缩,我可以用PHP中的gzdecode()解压缩。
but it doesn't work... 但它不起作用......
my functions compress() and decompress() works fine inside my Java App, but when I send it to webservice, something goes wrong and gzdecode() doesn't work. 我的函数compress()和decompress()在我的Java App中运行良好,但是当我将它发送到webservice时,出现问题并且gzdecode()不起作用。 I have no idea what I missing, I need some help
我不知道我错过了什么,我需要一些帮助
functions used in java app (client) java app(client)中使用的函数
public String Post(){
String retorno = "";
String u = compress(getInput());
u = URLEncoder.encode(URLEncoder.encode(u, "UTF-8"));
URL uri = new URL(url + u);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setDoOutput(false);
conn.setRequestMethod(getMethod());
conn.setRequestProperty("Content-encoding", "gzip");
conn.setRequestProperty("Content-type", "application/octet-stream");
BufferedReader buffer = new BufferedReader(
new InputStreamReader((conn.getInputStream())));
String r = "";
while ((r = buffer.readLine()) != null) {
retorno = r + "\n";
}
return retorno;
}
GZIP compress function (client) GZIP压缩功能(客户端)
public static String compress(String str) throws IOException {
byte[] blockcopy = ByteBuffer
.allocate(4)
.order(java.nio.ByteOrder.LITTLE_ENDIAN)
.putInt(str.length())
.array();
ByteArrayOutputStream os = new ByteArrayOutputStream(str.length());
GZIPOutputStream gos = new GZIPOutputStream(os);
gos.write(str.getBytes());
gos.close();
os.close();
byte[] compressed = new byte[4 + os.toByteArray().length];
System.arraycopy(blockcopy, 0, compressed, 0, 4);
System.arraycopy(os.toByteArray(), 0, compressed, 4,
os.toByteArray().length);
return Base64.encode(compressed);
}
method php used to receive a URL (server, using Slim/PHP Framework) 方法php用于接收URL(服务器,使用Slim / PHP Framework)
init::$app->post('/enviar/:obj/', function( $obj ) {
$dec = base64_decode(urldecode( $obj ));//decode url and decode base64 tostring
$dec = gzdecode($dec);//here is my problem, gzdecode() doesn't work
}
post method 发布方法
public Sender() throws JSONException {
//
url = "http://192.168.0.25/api/index.php/enviar/";
method = "POST";
output = true;
//
}
As noticed in some of the comments. 正如一些评论中所注意到的那样。
Bigger data should be send as a POST request instead of GET. 较大的数据应作为POST请求而不是GET发送。 URL params should be used only for single variables.
URL参数只能用于单个变量。 As you noticed the URL length is limited to few kB and it's not very good idea to send larger data this way (even though GZIP compressed).
正如您所注意到的,URL长度限制为几KB,以这种方式发送更大的数据并不是一个好主意(即使GZIP压缩)。
Your GZIP compression code seems to be wrong. 您的GZIP压缩代码似乎是错误的。 Please try this:
请试试这个:
public static String compress(String str) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream(str.length());
GZIPOutputStream gos = new GZIPOutputStream(os);
gos.write(str.getBytes());
os.close();
gos.close();
return Base64.encodeToString(os.toByteArray(),Base64.DEFAULT);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.