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

java发送http请求,内容为xml格式&&传统URI类请求的更多相关文章

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

    import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStr ...

  2. Java发HTTP POST请求(内容为xml格式)

    Java发HTTP POST请求(内容为xml格式) 一.POST请求 服务器地址:http://5.0.217.50:17001/VideoSend 服务器提供的是xml格式的http接口,接口定义 ...

  3. ajax数据请求4(xml格式)

    ajax数据请求4(xml格式): <!doctype html> <html> <head> <meta charset="utf-8" ...

  4. python接口自动化(四十一)- 发xml格式参数的post请求(超详解)

    简介 最近在工作中,遇到一种奇葩的接口,它的参数数据是通过xml,进行传递的,不要大惊小怪的,林子大了什么鸟都有,每个人的思路想法不一样,开发的接口也是各式各样的,如果想要统一的话,必须是提前团队已经 ...

  5. Java发送HTTP POST请求(内容为xml格式)

    今天在给平台用户提供http简单接口的时候,顺便写了个调用的Java类供他参考.      服务器地址:http://5.0.217.50:17001/VideoSend 服务器提供的是xml格式的h ...

  6. Java POI 读取Excel数据转换为XML格式

    1.首先要下载poi相关的包:http://poi.apache.org/  ,以下是所需的jar包 2.贴上详细的代码 public class ExcelToXml { /** * 将excel的 ...

  7. 将Java对象序列化成JSON和XML格式

    1.先定义一个Java对象Person: public class Person { String name; int age; int number; public String getName() ...

  8. XML格式与实体类的转换

    背景 本人头一回写博客,请大家多多关照.通过读取XML文件获取用户管理权限,其中涉及三部分: 1.XML文件的生成: 2.XML文件的读取: 3.XML文件的保存: 如何做 第一步:自己先将XML文件 ...

  9. Soup协议-即普通post请求,内容域xml

    1.基础问题 1.1 soup-Simple Object Access Protocal简单对象访问协议 a).承载在http协议之上,http支持传输img/html/文件等,soup请求和响应域 ...

随机推荐

  1. numpy 小示例

    import numpy as np 生成 3*4 的由  0 组成的二维数组 >>> np.zeros((3,4)) array([[0., 0., 0., 0.], [0., 0 ...

  2. 数据结构(C语言)关于查找与排序

    1)利用readData()函数从data1.txt中读入不同规模的数据存入数组,编写基于数组的顺序查找算法,测试数据量为1万.5万.10万.20万.30万.40万和50万时的数据查询时间. 算法代码 ...

  3. Maven中使用Jetty容器

    1.在pom.xml中添加Jetty的插件 <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId ...

  4. python查看文件的编码格式

    pip install chardet 执行 import chardet f = open('a.doc',r) data = f.read() print chardet.detect(data) ...

  5. zabbix监控系统的配置

    一.zabbix介绍 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系 ...

  6. JavaScript事件漫谈

    内容概要: event对象,事件在多个浏览器中的兼容,事件的传播机制,JS自定义事件,jQuery的自定义事件的绑定与触发 Event对象 Event对象属于HTML DOM对象.Event 对象代表 ...

  7. php 给对象动态增加属性 及子类继承父类的构造方法

    <?php error_reporting(-1); ini_set('display_errors','on'); class A { public $a = 'hello'; public  ...

  8. 了解WCF的前世今生之实现服务端(一)

    http://www.cnblogs.com/jiagoushi/archive/2013/03/15/2962351.html 1.WCF是对现有的分布式通信技术的一个整合,其中包括Com/DCom ...

  9. magento优化

    magento -- SEO优化继续往前走一步 magento的SEO做的实在太好了,让其它的电子商务平台与之相比实在是不是一个重级的,这也许就是magento成功的原因.尽管现在结合apache,实 ...

  10. 清除的通用样式 css

    /*公共样式--开始*/ html, body, div, ul, li, h1, h2, h3, h4, h5, h6, p, dl, dt, dd, ol, form, input, textar ...