对于java用发送http请求,请求内容为xml格式

  1.   
  2. import java.io.BufferedInputStream;  
  3. import java.io.BufferedReader;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.io.InputStreamReader;  
  8. import java.io.OutputStreamWriter;  
  9. import java.net.URI;  
  10. import java.net.URL;  
  11. import java.net.URLConnection;  
  12.   
  13. import org.apache.commons.httpclient.HttpClient;  
  14. import org.apache.commons.httpclient.HttpStatus;  
  15. import org.apache.commons.httpclient.methods.PostMethod;  
  16.   
  17. /** 
  18.  * 测试调用一些meeting第三方接口 
  19.  * @author Jack.Song 
  20.  */  
  21. public class TestMeetingInterface {  
  22.   
  23.     /** 
  24.      * @param args 
  25.      */  
  26.     public static void main(String[] args) {  
  27.           
  28.         String url = "http://192.168.0.68/integration/xml";  
  29.         TestMeetingInterface tmi = new TestMeetingInterface();  
  30.         System.out.println(tmi.post(url,"listSummaryMeeting.xml"));  
  31.           
  32.         /*//判断当前系统是否支持Java AWT Desktop扩展 
  33.         if(java.awt.Desktop.isDesktopSupported()){ 
  34.             try { 
  35.                 URI path = tmi.getClass().getResource("/listSummaryMeeting.xml").toURI(); 
  36.                 System.out.println(path); 
  37.                 //创建一个URI实例 
  38. //              java.net.URI uri = java.net.URI.create(path);  
  39.                 //获取当前系统桌面扩展 
  40.                 java.awt.Desktop dp = java.awt.Desktop.getDesktop(); 
  41.                 //判断系统桌面是否支持要执行的功能 
  42.                 if(dp.isSupported(java.awt.Desktop.Action.BROWSE)){ 
  43.                     //获取系统默认浏览器打开链接 
  44.                     dp.browse(path);     
  45.                 } 
  46.             } catch (Exception e) { 
  47.                 e.printStackTrace(); 
  48.             }              
  49.         }*/  
  50.     }  
  51.       
  52.       
  53.   
  54.     /**  
  55.      * 发送xml数据请求到server端  
  56.      * @param url xml请求数据地址  
  57.      * @param xmlString 发送的xml数据流  
  58.      * @return null发送失败,否则返回响应内容  
  59.      */    
  60.     public String post(String url,String xmlFileName){    
  61.         //关闭   
  62.         System.setProperty("org.apache.commons.logging.Log""org.apache.commons.logging.impl.SimpleLog");     
  63.         System.setProperty("org.apache.commons.logging.simplelog.showdatetime""true");     
  64.         System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient""stdout");    
  65.           
  66.         //创建httpclient工具对象   
  67.         HttpClient client = new HttpClient();    
  68.         //创建post请求方法   
  69.         PostMethod myPost = new PostMethod(url);    
  70.         //设置请求超时时间   
  71.         client.setConnectionTimeout(300*1000);  
  72.         String responseString = null;    
  73.         try{    
  74.             //设置请求头部类型   
  75.             myPost.setRequestHeader("Content-Type","text/xml");  
  76.             myPost.setRequestHeader("charset","utf-8");  
  77.               
  78.             //设置请求体,即xml文本内容,注:这里写了两种方式,一种是直接获取xml内容字符串,一种是读取xml文件以流的形式   
  79. //          myPost.setRequestBody(xmlString);   
  80.               
  81.             InputStream body=this.getClass().getResourceAsStream("/"+xmlFileName);  
  82.             myPost.setRequestBody(body);  
  83. //            myPost.setRequestEntity(new StringRequestEntity(xmlString,"text/xml","utf-8"));     
  84.             int statusCode = client.executeMethod(myPost);    
  85.             if(statusCode == HttpStatus.SC_OK){    
  86.                 BufferedInputStream bis = new BufferedInputStream(myPost.getResponseBodyAsStream());    
  87.                 byte[] bytes = new byte[1024];    
  88.                 ByteArrayOutputStream bos = new ByteArrayOutputStream();    
  89.                 int count = 0;    
  90.                 while((count = bis.read(bytes))!= -1){    
  91.                     bos.write(bytes, 0, count);    
  92.                 }    
  93.                 byte[] strByte = bos.toByteArray();    
  94.                 responseString = new String(strByte,0,strByte.length,"utf-8");    
  95.                 bos.close();    
  96.                 bis.close();    
  97.             }    
  98.         }catch (Exception e) {    
  99.             e.printStackTrace();    
  100.         }    
  101.         myPost.releaseConnection();    
  102.         return responseString;    
  103.     }    
  104.       
  105.     /** 
  106.      * 用传统的URI类进行请求 
  107.      * @param urlStr 
  108.      */  
  109.     public void testPost(String urlStr) {  
  110.         try {  
  111.             URL url = new URL(urlStr);  
  112.             URLConnection con = url.openConnection();  
  113.             con.setDoOutput(true);  
  114.             con.setRequestProperty("Pragma:""no-cache");  
  115.             con.setRequestProperty("Cache-Control""no-cache");  
  116.             con.setRequestProperty("Content-Type""text/xml");  
  117.   
  118.             OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());      
  119.             String xmlInfo = getXmlInfo();  
  120.             System.out.println("urlStr=" + urlStr);  
  121. //            System.out.println("xmlInfo=" + xmlInfo);   
  122.             out.write(new String(xmlInfo.getBytes("UTF-8")));  
  123.             out.flush();  
  124.             out.close();  
  125.             BufferedReader br = new BufferedReader(new InputStreamReader(con  
  126.                     .getInputStream()));  
  127.             String line = "";  
  128.             for (line = br.readLine(); line != null; line = br.readLine()) {  
  129.                 System.out.println(line);  
  130.             }  
  131.         } catch (Exception e) {  
  132.             e.printStackTrace();  
  133.         }  
  134.     }  
  135.   
  136.     private String getXmlInfo() {  
  137.         StringBuilder sb = new StringBuilder();  
  138.         sb.append("<?xml version='1.0' encoding='UTF-8'?>");  
  139.         sb.append("<Message>");  
  140.         sb.append(" <header>");  
  141.         sb.append("     <action>readMeetingStatus</action>");  
  142.         sb.append("     <service>meeting</service>");  
  143.         sb.append("     <type>xml</type>");  
  144.         sb.append("     <userName>admin</userName>");  
  145.         sb.append("     <password>admin</password>");  
  146.         sb.append("     <siteName>box</siteName>");  
  147.         sb.append(" </header>");  
  148.         sb.append(" <body>");  
  149.         sb.append("     <confKey>43283344</confKey>");  
  150.         sb.append(" </body>");  
  151.         sb.append("</Message>");  
  152.           
  153.         return sb.toString();  
  154.     }  
  155. }  
posted @ 2013-06-26 20:17  爱生活,爱编程  阅读(33665)  评论(0编辑  收藏  举报