httpclient發送xml字符串(推送)

public static void main(String[] args) {
String xml = "<?xml version="
+ "\"1.0\""
+ " encoding="
+ "\"UTF-8\""
+ "?><SDRequest><TransactionName>CreateDataFileComplete</TransactionName><IdentityInfo><Code>"
+ 1 + "</Code><Description></Description><Timestamp>"
+ "20100315140542" + "</Timestamp></IdentityInfo></SDRequest>";//新接的一個項目接口,非要用xml請求,找不到別的post方式,最終選用這種方式,將參數拼成xml字符串

// File input = new File("test.xml");//若是是xml文件,能夠這樣寫
PostMethod post = new PostMethod("http://localhost/site/forXls.do");//請求地址

// 設置請求的內容直接從文件中讀取
   //   post.setRequestBody( new FileInputStream(input)); 
     // if (input.length() < Integer.MAX_VALUE)
     //    post.setRequestContentLength(input.length());
    //  else
     //  post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);

post.setRequestBody(xml);//這裏添加xml字符串

// 指定請求內容的類型
post.setRequestHeader("Content-type", "text/xml; charset=GBK");
HttpClient httpclient = new HttpClient();//建立 HttpClient 的實例
int result;
try {
result = httpclient.executeMethod(post);
System.out.println("Response status code: " + result);//返回200爲成功
System.out.println("Response body: ");
System.out.println(post.getResponseBodyAsString());//返回的內容
post.releaseConnection();//釋放鏈接
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

//以上就是發送請求的代碼,對方接收到數據能夠直接解析成xml

Document airDocument = getClientRequestMessage(request);
System.out.println(airDocument.getRootElement().getName());
if ((XmlInterfaceParam.SD_REQUEST).equals(airDocument.getRootElement()
.getName())) {
Element tn = airDocument.getRootElement().getChild(
XmlInterfaceParam.TRANSACTION_NAME);
if ("CreateDataFileComplete".equals(tn.getText())) {
Element ii = airDocument.getRootElement().getChild(
XmlInterfaceParam.IDENTITY_INFO);
String code = ii.getChildText(XmlInterfaceParam.CODE);
String description = ii
.getChildText(XmlInterfaceParam.DESCRIPTION);
String timestamp = ii.getChildText(XmlInterfaceParam.TIMESTAMP);
System.out.println("code: " + code);
System.out.println("description: " + description);
System.out.println("timestamp: " + timestamp);
}
}

// 讀取xml
private Document getClientRequestMessage(HttpServletRequest _request)
throws UnexpectedException {
try {
SAXBuilder builder = new SAXBuilder();
InputSource is = new InputSource(); // create an input
// source
is.setByteStream(_request.getInputStream()); // set the input
// stream mandated
// to UTF-8
is.setEncoding("UTF-8"); // set the mandate
// encoding to the input
// source
Document document = builder.build(is);
return document;
} catch (IOException e) {
e.printStackTrace();
throw new UnexpectedException(
"IOException exception when getInputStream from http request",
e);
} catch (JDOMException e) {
e.printStackTrace();
throw new UnexpectedException(
"JDOMException when build document form inputstream", e);
} catch (NullPointerException e) {
e.printStackTrace();
throw new UnexpectedException(
"NullPointerException when build document form inputstream",
e);
} catch (ClassCastException e) {
e.printStackTrace();
throw new UnexpectedException(
"ClassCastException when build document form inputstream",
e);
}
}post