liuhaifeng315
2017-04-06 16:02:54
java servlet后台调用接口返回xml给客户端
工作好几年 调用接口大部分都是json这种,突然蹦出个xml一下傻逼了 都不知道怎么传递给别人了。 后面百度了下发现 接口还是调用接口 只不过需要在后台设置header
//创建httpclient工具对象 HttpClient client = new HttpClient(); //创建post请求方法 PostMethod myPost = new PostMethod(url);
myPost.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); myPost.setRequestHeader("charset","utf-8");
//xml拼成string
// myPost.setRequestBody(xmlString);
//文件流方式
// InputStream body=this.getClass().getResourceAsStream("/"+xmlFileName);
// myPost.setRequestBody(body);
//在处理自动提交表单的业务中往往要用到httpclient的poseMethod提交数据,httpclient3.1采用RequestEntity接口替代以前的RequestBody。
myPost.setRequestEntity(new StringRequestEntity(xmlString,"text/xml","utf-8"));
这样就吧xml发送给了对方
接收xml的方法
public void sendMedia(HttpServletRequest request, HttpServletResponse response){ SAXReader saxReader = null; Document doc = null; try { saxReader = new SAXReader(); doc = saxReader.read(request.getInputStream()); String xmlStr = doc.asXML(); //解析xml } catch (Exception e) { e.printStackTrace(); } }
评论

ouyangwen
2020年5月18日
暂无贡献等级
AllenZ
2020年3月4日
暂无贡献等级
yhj123456
2019年12月31日
暂无贡献等级
ausion19
2019年12月6日
暂无贡献等级
2996321909 LV1
2019年11月26日
chenlijuan
2019年10月15日
暂无贡献等级
qiuwx0210
2019年10月13日
暂无贡献等级
邹宜珂
2019年8月7日
暂无贡献等级
1007586545 LV1
2019年7月23日
wenzhou LV4
2019年4月25日