xmlrpc调用可以使用压缩
xmlrpc的报文的冗余是很多的,大量的标签将会消耗掉你的带宽,所以对xmlrpc的返回的报文进行压缩很有必要的。以前写的代码:
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import org.apache.xmlrpc.XmlRpcException;
- import org.apache.xmlrpc.client.XmlRpcClient;
- import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
- public class testFindPersonByFirstName {
- /**
- * @param args
- * @throws MalformedURLException
- * @throws XmlRpcException
- */
- public static void main(String[] args) throws MalformedURLException, XmlRpcException {
- // TODO Auto-generated method stub
- XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
- config.setServerURL(new URL("http://127.0.0.1:18080/webtools/control/xmlrpc"));
- XmlRpcClient client = new XmlRpcClient();
- client.setConfig(config);
- Object[] params = new Object[]{"John"};
- HashMap result = (HashMap) client.execute("findPersonByfirstName", params);
- Object[] oz = (Object [])result.get("personList");
- System.out.println("partyId firstName lastName");
- for(int i=0;i<oz.length;i++){
- HashMap person = (HashMap)oz[i];
- System.out.println(person.get("partyId")+" "+person.get("firstName")+" "+person.get("lastName"));
- }
- }
- }
我们用TcpMon抓一下包,返回的数据:
- HTTP/1.1 200 OK
- Server: Apache-Coyote/1.1
- Set-Cookie: JSESSIONID=B5E7115DD5E2568D75AF6CD19B0DB2E4.jvm1; Path=/webtools
- Set-Cookie: OFBiz.Visitor=10311; Expires=Sat, 28-Nov-2009 07:57:12 GMT; Path=/
- Content-Type: text/xml;charset=UTF-8
- Transfer-Encoding: chunked
- Date: Fri, 28 Nov 2008 07:57:12 GMT
- 1011
- <?xml version="1.0" encoding="UTF-8"?><methodResponse><params><param><value><struct><member><name>personList</name><value><array><data><value><struct><member><name>partyId</name><value>10000</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10001</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10002</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10003</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10004</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10010</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10020</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10030</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10040</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10050</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10060</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10070</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10071</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10072</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10080</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10081</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10082</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10090</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10100</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value></data></array></value></member></struct></value></param></params></methodResponse>
- 0
我们修改一下代码,增加一行:
- config.setGzipRequesting(true);