import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod; /**
* 测试调用一些meeting第三方接口
* @author Jack.Song
*/
public class TestMeetingInterface { /**
* @param args
*/
public static void main(String[] args) { String url = "http://192.168.0.68/integration/xml";
TestMeetingInterface tmi = new TestMeetingInterface();
System.out.println(tmi.post(url,"listSummaryMeeting.xml")); /*//判断当前系统是否支持Java AWT Desktop扩展
if(java.awt.Desktop.isDesktopSupported()){
try {
URI path = tmi.getClass().getResource("/listSummaryMeeting.xml").toURI();
System.out.println(path);
//创建一个URI实例
// java.net.URI uri = java.net.URI.create(path);
//获取当前系统桌面扩展
java.awt.Desktop dp = java.awt.Desktop.getDesktop();
//判断系统桌面是否支持要执行的功能
if(dp.isSupported(java.awt.Desktop.Action.BROWSE)){
//获取系统默认浏览器打开链接
dp.browse(path);
}
} catch (Exception e) {
e.printStackTrace();
}
}*/
} /**
* 发送xml数据请求到server端
* @param url xml请求数据地址
* @param xmlString 发送的xml数据流
* @return null发送失败,否则返回响应内容
*/
public String post(String url,String xmlFileName){
//关闭
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "stdout"); //创建httpclient工具对象
HttpClient client = new HttpClient();
//创建post请求方法
PostMethod myPost = new PostMethod(url);
//设置请求超时时间
client.setConnectionTimeout(300*1000);
String responseString = null;
try{
//设置请求头部类型
myPost.setRequestHeader("Content-Type","text/xml");
myPost.setRequestHeader("charset","utf-8"); //设置请求体,即xml文本内容,注:这里写了两种方式,一种是直接获取xml内容字符串,一种是读取xml文件以流的形式
// myPost.setRequestBody(xmlString); InputStream body=this.getClass().getResourceAsStream("/"+xmlFileName);
myPost.setRequestBody(body);
// myPost.setRequestEntity(new StringRequestEntity(xmlString,"text/xml","utf-8"));
int statusCode = client.executeMethod(myPost);
if(statusCode == HttpStatus.SC_OK){
BufferedInputStream bis = new BufferedInputStream(myPost.getResponseBodyAsStream());
byte[] bytes = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int count = 0;
while((count = bis.read(bytes))!= -1){
bos.write(bytes, 0, count);
}
byte[] strByte = bos.toByteArray();
responseString = new String(strByte,0,strByte.length,"utf-8");
bos.close();
bis.close();
}
}catch (Exception e) {
e.printStackTrace();
}
myPost.releaseConnection();
return responseString;
} /**
* 用传统的URI类进行请求
* @param urlStr
*/
public void testPost(String urlStr) {
try {
URL url = new URL(urlStr);
URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setRequestProperty("Pragma:", "no-cache");
con.setRequestProperty("Cache-Control", "no-cache");
con.setRequestProperty("Content-Type", "text/xml"); OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
String xmlInfo = getXmlInfo();
System.out.println("urlStr=" + urlStr);
// System.out.println("xmlInfo=" + xmlInfo);
out.write(new String(xmlInfo.getBytes("UTF-8")));
out.flush();
out.close();
BufferedReader br = new BufferedReader(new InputStreamReader(con
.getInputStream()));
String line = "";
for (line = br.readLine(); line != null; line = br.readLine()) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
} private String getXmlInfo() {
StringBuilder sb = new StringBuilder();
sb.append("<?xml version='1.0' encoding='UTF-8'?>");
sb.append("<Message>");
sb.append(" <header>");
sb.append(" <action>readMeetingStatus</action>");
sb.append(" <service>meeting</service>");
sb.append(" <type>xml</type>");
sb.append(" <userName>admin</userName>");
sb.append(" <password>admin</password>");
sb.append(" <siteName>box</siteName>");
sb.append(" </header>");
sb.append(" <body>");
sb.append(" <confKey>43283344</confKey>");
sb.append(" </body>");
sb.append("</Message>"); return sb.toString();
}
}

最新文章

  1. [翻译].NET随机数
  2. C#设计模式(23)——备忘录模式(Memento Pattern)
  3. C++引用和java引用的区别
  4. Linux中安装Cisco Packet Tracer
  5. ASP.NET 5 之 错误诊断和它的中间件们
  6. CocoaPod安装
  7. HTTPS那些事(一) HTTPS原理
  8. php数组array,知道键名如何提取键值
  9. 基于visual Studio2013解决面试题之0905子串数量
  10. 验证码计时 -- UIButton setTitle 闪烁问题解决方案
  11. SQL 合并列值和拆分列值
  12. tcpdump的表达元
  13. Apollo与ROS
  14. [LeetCode] Minimum Cost to Merge Stones 混合石子的最小花费
  15. leetcode — merge-intervals
  16. mysql设计表时注意事项
  17. js传递数组
  18. 第二次项目冲刺(Beta版本)2017/12/6
  19. MetaMask/provider-engine-1
  20. java (10) 集合类

热门文章

  1. 定义与声明、头文件与extern总结
  2. Ubuntu下允许Root用户的操作 (图形界面登录、su切换……)
  3. Hadoop开发相关问题
  4. 关于Unity中常用的数据结构和JSON处理(专题三)
  5. e870. 获取默认外观的数据
  6. C# 委托的同步调用和异步调用
  7. 【设计和开发一套简单自己主动化UI框架】
  8. RMAN命令总结
  9. win7下安装双系统Ubuntu14.04后开机没有win7,直接进入Ubuntu
  10. jquery click事件,多次执行