版权声明:本文为博主原创文章,转载请注明源地址。 https://cloud.tencent.com/developer/article/1433720
使用axis2的ServiceClient,以RPC或Stub方式实现webservice调用时,如果要对数据进行GZIP压缩,也挺简单,只要给ServiceClient设置 MC_GZIP_REQUEST和MC_ACCEPT_GZIP 属性就可以了。
示例代码如下:
Options options = serviceClient.getOptions();
options.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);
options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, "true");
options.setProperty(HTTPConstants.CHUNKED, "true");
// Request(请求)数据用GZIP压缩
options.setProperty(HTTPConstants.MC_GZIP_REQUEST, Boolean.TRUE);
// 向服务器声明接受GZIP压缩
options.setProperty(HTTPConstants.MC_ACCEPT_GZIP , Boolean.TRUE);
关于MC_GZIP_REQUEST和MC_ACCEPT_GZIP 属性的说明参见org.apache.axis2.transport.http.HTTPConstants代码中的注释:
/**
* If you want the HTTP sender to indicate that it can accept a gziped
* response, set this message context property to true. The sender will
* automatically unzip the response if its gzipped.
*/
public static final String MC_ACCEPT_GZIP = "transport.http.acceptGzip";
/**
* by default the HTTP request body is not compressed. set this message
* context property to true to have the request body gzip compressed.
*/
public static final String MC_GZIP_REQUEST = "transport.http.gzipRequest";
以及方法void org.apache.axis2.client.Options.setProperty(String propertyKey, Object property)的说明:
org.apache.axis2.transport.http.HTTPConstants.MC_GZIP_REQUEST
If set this will GZip your request and send over to the destination. Before doing this, you must make sure that the receiving end supports GZip compressed streams.
Possible values are:
"true"/"false" or Boolean.TRUE/Boolean.FALSE
org.apache.axis2.transport.http.HTTPConstants.MC_ACCEPT_GZIP
Whether or not you send a gzip-ped request, you can choose to receive GZIP back from the server using this flag.
Possible values are:
"true"/"false" or Boolean.TRUE/Boolean.FALSE