用JAVA发送一个XML格式的HTTP请求
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 }
**************胜者先胜而后求战,败者先战而后求胜!**************