分享

客户端post带参数URL+XML,服务器端读取

 昵称20874412 2015-05-03
客户端:

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();
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多