1. 程式人生 > >用JAVA發送一個XML格式的HTTP請求

用JAVA發送一個XML格式的HTTP請求

response ops pri source methods mar 桌面 reader ase

  1 import java.io.BufferedInputStream;  
  2 import java.io.BufferedReader;  
  3 import java.io.ByteArrayOutputStream;  
  4 import java.io.IOException;  
  5 import java.io.InputStream;  
  6 import java.io.InputStreamReader;  
  7 import java.io.OutputStreamWriter;  
  8 import java.net.URI;  
9 import java.net.URL; 10 import java.net.URLConnection; 11 12 import org.apache.commons.httpclient.HttpClient; 13 import org.apache.commons.httpclient.HttpStatus; 14 import org.apache.commons.httpclient.methods.PostMethod; 15 16 /** 17 * 測試調用一些meeting第三方接口 18 * @author Jack.Song
19 */ 20 public class TestMeetingInterface { 21 22 /** 23 * @param args 24 */ 25 public static void main(String[] args) { 26 27 String url = "http://192.168.0.68/integration/xml"; 28 TestMeetingInterface tmi = new TestMeetingInterface();
29 System.out.println(tmi.post(url,"listSummaryMeeting.xml")); 30 31 /*//判斷當前系統是否支持Java AWT Desktop擴展 32 if(java.awt.Desktop.isDesktopSupported()){ 33 try { 34 URI path = tmi.getClass().getResource("/listSummaryMeeting.xml").toURI(); 35 System.out.println(path); 36 //創建一個URI實例 37 // java.net.URI uri = java.net.URI.create(path); 38 //獲取當前系統桌面擴展 39 java.awt.Desktop dp = java.awt.Desktop.getDesktop(); 40 //判斷系統桌面是否支持要執行的功能 41 if(dp.isSupported(java.awt.Desktop.Action.BROWSE)){ 42 //獲取系統默認瀏覽器打開鏈接 43 dp.browse(path); 44 } 45 } catch (Exception e) { 46 e.printStackTrace(); 47 } 48 }*/ 49 } 50 51 52 53 /** 54 * 發送xml數據請求到server端 55 * @param url xml請求數據地址 56 * @param xmlString 發送的xml數據流 57 * @return null發送失敗,否則返回響應內容 58 */ 59 public String post(String url,String xmlFileName){ 60 //關閉 61 System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); 62 System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); 63 System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "stdout"); 64 65 //創建httpclient工具對象 66 HttpClient client = new HttpClient(); 67 //創建post請求方法 68 PostMethod myPost = new PostMethod(url); 69 //設置請求超時時間 70 client.setConnectionTimeout(300*1000); 71 String responseString = null; 72 try{ 73 //設置請求頭部類型 74 myPost.setRequestHeader("Content-Type","text/xml"); 75 myPost.setRequestHeader("charset","utf-8"); 76 77 //設置請求體,即xml文本內容,註:這裏寫了兩種方式,一種是直接獲取xml內容字符串,一種是讀取xml文件以流的形式 78 // myPost.setRequestBody(xmlString); 79 80 InputStream body=this.getClass().getResourceAsStream("/"+xmlFileName); 81 myPost.setRequestBody(body); 82 // myPost.setRequestEntity(new StringRequestEntity(xmlString,"text/xml","utf-8")); 83 int statusCode = client.executeMethod(myPost); 84 if(statusCode == HttpStatus.SC_OK){ 85 BufferedInputStream bis = new BufferedInputStream(myPost.getResponseBodyAsStream()); 86 byte[] bytes = new byte[1024]; 87 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 88 int count = 0; 89 while((count = bis.read(bytes))!= -1){ 90 bos.write(bytes, 0, count); 91 } 92 byte[] strByte = bos.toByteArray(); 93 responseString = new String(strByte,0,strByte.length,"utf-8"); 94 bos.close(); 95 bis.close(); 96 } 97 }catch (Exception e) { 98 e.printStackTrace(); 99 } 100 myPost.releaseConnection(); 101 return responseString; 102 } 103 104 /** 105 * 用傳統的URI類進行請求 106 * @param urlStr 107 */ 108 public void testPost(String urlStr) { 109 try { 110 URL url = new URL(urlStr); 111 URLConnection con = url.openConnection(); 112 con.setDoOutput(true); 113 con.setRequestProperty("Pragma:", "no-cache"); 114 con.setRequestProperty("Cache-Control", "no-cache"); 115 con.setRequestProperty("Content-Type", "text/xml"); 116 117 OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream()); 118 String xmlInfo = getXmlInfo(); 119 System.out.println("urlStr=" + urlStr); 120 // System.out.println("xmlInfo=" + xmlInfo); 121 out.write(new String(xmlInfo.getBytes("UTF-8"))); 122 out.flush(); 123 out.close(); 124 BufferedReader br = new BufferedReader(new InputStreamReader(con 125 .getInputStream())); 126 String line = ""; 127 for (line = br.readLine(); line != null; line = br.readLine()) { 128 System.out.println(line); 129 } 130 } catch (Exception e) { 131 e.printStackTrace(); 132 } 133 } 134 135 private String getXmlInfo() { 136 StringBuilder sb = new StringBuilder(); 137 sb.append("<?xml version=‘1.0‘ encoding=‘UTF-8‘?>"); 138 sb.append("<Message>"); 139 sb.append(" <header>"); 140 sb.append(" <action>readMeetingStatus</action>"); 141 sb.append(" <service>meeting</service>"); 142 sb.append(" <type>xml</type>"); 143 sb.append(" <userName>admin</userName>"); 144 sb.append(" <password>admin</password>"); 145 sb.append(" <siteName>box</siteName>"); 146 sb.append(" </header>"); 147 sb.append(" <body>"); 148 sb.append(" <confKey>43283344</confKey>"); 149 sb.append(" </body>"); 150 sb.append("</Message>"); 151 152 return sb.toString(); 153 } 154 }

用JAVA發送一個XML格式的HTTP請求