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. 攻防:文件上传漏洞的攻击与防御,转自H3C

    WebShell就是以asp.php.jsp或者cgi等网页文件形式存在的一种命令执行环境,也可以将其称做为一种网页后门.黑客在入侵了一个网站后,通常会将这些asp或php后门文件与网站服务器WEB目 ...

  2. php 从2维数组组合为四维数组分析

  3. REST easy with kbmMW #4 – Access management

    在前面有关如何使用kbmMW创建REST服务器的基础上,现在已经到了考虑该如何控制用户的访问.什么是访问管理?就是“允许谁做什么"的问题. 显然,这个世界中存在数据,应该保护他而不被未授权的 ...

  4. Linux中MySQL中文乱码问题

    一. 问题描述 登录后查看mysql默认编码: mysql> show variables like 'character%'; +--------------------------+---- ...

  5. SWIFT中获取当前经伟度

    很多的APP中都会用到用户的当前位置信息,本文将实现这个小功能 import UIKit import CoreLocation //添加引用 class ViewController: UIView ...

  6. 外部获取IndexPath的几种方式(关联对象等)

    1. 一般方式 - (void)buttonAction:(UIButton *)sender { UITableViewCell *cell = (UITableViewCell *)[[sende ...

  7. 端到端文本识别CRNN论文解读

    CRNN 论文: An End-to-End Trainable Neural Network for Image-based Sequence Recognition and Its Applica ...

  8. Maven3的环境配置

    1 需要准备一个Maven的包,名字叫:apache_maven_3.3.9(在百度中搜索自行下载) 2 在eclipse中点击window→preferences→maven→Installatio ...

  9. DS18B20配置

    复位脉冲: 先拉低至少480us,以产生复位脉冲,接着释放4.7k电阻为高,延时15~60us, 进入接收. void DS18B20_Rst(void) { DS18B20_IO_OUT(); // ...

  10. 我的第一个Mybatis程序

    第一个Mybatis程序 在JDBC小结中(可以参阅本人JDBC系列文章),介绍到了ORM,其中Mybatis就是一个不错的ORM框架 MyBatis由iBatis演化而来 iBATIS一词来源于“i ...