在工作中有用到HTTP请求外部地址传递数据. 用Url这个类明显效率低下,所以改用连接池概念的URL请求,这将大大提高了请求效率. 主发送类 [java] view plain copy package com.cdg.test; import java.util.HashMap; import java.util.Map; import or
客户端发送请求、以及处理服务端响应代码为://发送xml请求
URL url = new URL(“http://127.0.0.1:8080/Test/sendXmlAndReturnXml.do”);
String xml = “cccccc客户端请求的xml数据cccccccc”;
URLConnection conn = null;
conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty(“Content-Length”, Integer.toString(xml.length()));
conn.setRequestProperty(“Content-Type”, “text/xml; charset=utf-8”);
OutputStream ops = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(ops, “GBK”);
osw.write(xml);
osw.flush();
osw.close(