客户端: import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; public class HttpClientTest { String data = "<root><header><type>fetch</type></header><content><program>test</program></content></root>"; String url = "http://localhost:8080/SgWebSer/AppStatus?username=cc&pwd=11"; public void postSend() { HttpClient httpclient = new HttpClient(); PostMethod post = new PostMethod(url); String info = null; try { RequestEntity entity = new StringRequestEntity(data, "text/xml", "iso-8859-1"); post.setRequestEntity(entity); httpclient.executeMethod(post); int code = post.getStatusCode(); if (code == HttpStatus.SC_OK) info = new String(post.getResponseBodyAsString()); } catch (Exception ex) { ex.printStackTrace(); } finally { post.releaseConnection(); } } public static void main (String[] args) { HttpClientTest hct = new HttpClientTest(); hct.postSend(); } } 服务器端 doPost String username = request.getParameter("username"); 用的都是Jdom哦 SAXBuilder builder = new SAXBuilder(); InputSource is = new InputSource(); try { is.setByteStream(request.getInputStream()); is.setEncoding("iso-8859-1"); Document document = builder.build(is); document.getRootElement().getChild("header").getChild("type").getText(); Format format = Format.getPrettyFormat(); format.setEncoding("UTF-8");// 设置xml文件的字符为UTF-8,解决中文问题 XMLOutputter xmlout = new XMLOutputter(format); ByteArrayOutputStream bo = new ByteArrayOutputStream(); xmlout.output(document, bo); String s = bo.toString(); System.out.println(bo.toString()); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } |
|
来自: 昵称20874412 > 《Web服务器》