JAVA 接受和发送HTTP POST XML

Exception rg.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;
}
}

你可能感兴趣的:(java)