httpclient發送xml字串(推送)

來源:互聯網
上載者:User

標籤:des   http   io   ar   os   sp   for   strong   檔案   

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

httpclient發送xml字串(推送)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.