使用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

本文介绍如何使用Axis2的ServiceClient通过设置属性实现请求数据的GZIP压缩发送及接收。通过简单的示例代码展示了如何启用GZIP压缩,并解释了相关属性的作用。
1187

被折叠的 条评论
为什么被折叠?



