JAVA 接受和发送HTTP POST XML

Exception :org.dom4j.DocumentException: Error on line -1 of document : Premature end of file. Nested exception: Premature end of file.

一、必须严格区分POST和GET
如果先执行request.getParameter("str");再解析POST过来的xml,则取POST的内容为空,dom4j解析xml会抛出如上异常。所以,应该把类HttpProcessor.java(如下)放在Servlet的doPost中调用,并且调用之前一定不能执行request.getParameter("str");


java服务器发送HTTP POST XML:

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class WeiBoClient {

String url;

public WeiBoClient(String url){

this.url = url;
}


public static void main(String[] args) {

String url = "http://localhost:9080/agent/agent";
WeiBoClient t = new WeiBoClient(url);
String visitorWeChatId = "o5L0Fjzr9J1o5_2X7a4NPfgZp17g";
String weixinXmlHead = "<xml><ToUserName>zhangsan</ToUserName><FromUserName>";
String weixinXmlEnd = "</FromUserName> <CreateTime>1348831860</CreateTime><MsgType>mestypecontent</MsgType><Content>12345</Content><MsgId>1234567890123456</MsgId></xml>";
t.sendMessage(weixinXmlHead + visitorWeChatId + weixinXmlEnd);
}

public void sendMessage(String urlparams) {

String returnStr = "";
HttpURLConnection httpConn = null;
InputStream fis = null;

try {

URL httpurl = new URL(url);
httpConn = (HttpURLConnection) httpurl.openConnection();

httpConn.setDoOutput(true);
httpConn.setRequestProperty("User-agent", "MSIE8.0"); //设置代理为IE8
httpConn.setRequestMethod("POST");
httpConn.setConnectTimeout(600000);
httpConn.setReadTimeout(600000);

OutputStream op = httpConn.getOutputStream();
op.write(urlparams.getBytes());


op.flush();
op.close();

if(httpConn.getResponseCode() == 200){
fis = httpConn.getInputStream();
returnStr = Stream2String(fis,"utf-8");
}
System.out.println(returnStr);

} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
if(fis!=null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
httpConn.disconnect();
}
}

private static String Stream2String(InputStream in, String encoding) {

if (in == null) {

return null;
}
StringBuffer out = new StringBuffer();
try {

char[] b = new char[1024];
InputStreamReader inread = new InputStreamReader(in, encoding);

for (int n; (n = inread.read(b)) != -1;) {
String line = new String(b, 0, n);
out.append(line);
}

} catch (Exception e) {

e.printStackTrace();
}

return out.toString();
}

}


java服务器接收解析HTTP POST 过来的 xml:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* 此类负责发送和解析:HTTP(POST) xml数据
*/
public class HttpProcessor {

protected static Logger log = LoggerFactory.getLogger(HttpProcessor.class.getName());

// private String weixinXml = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1348831860</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[this is a test]]></Content><MsgId>1234567890123456</MsgId></xml>";

/**
* 解析HttpServletRequest中POST过来的xml流
* @param request
* @param response
* @return a XML Document from the http
*/
public static Document getDocument(HttpServletRequest request, HttpServletResponse response) {

SAXReader saxReader = null;
Document doc = null;

try {
saxReader = new SAXReader();
doc = saxReader.read(request.getInputStream());

String xmlStr = doc.asXML();
log.warn("xml content: " + xmlStr);

} catch (Exception e) {
log.warn("Exception :" + e.toString());
StackTraceElement[] er = e.getStackTrace();
for (int i = 0; i < er.length; i++) {
log.info(er[i].toString());
}
}

return doc;
}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值