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. *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. ];
  76. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  77. ;
  78. ){
  79. , count);
  80. }
  81. byte[] strByte = bos.toByteArray();
  82. ,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格式的更多相关文章

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

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

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

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

  3. 如何设置Fiddler来拦截Java代码发送的HTTP请求,进行各种问题排查

    我们使用Java的RestTemplate或者Apache的HTTPClient编程的时候,经常遇到需要跟踪Java 代码发送的HTTP请求明细的情况.和javascript代码在浏览器里发送请求可以 ...

  4. java发送http请求,内容为xml格式&&传统URI类请求

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

  5. 提交post请求,参数为xml格式

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  6. 利用Ajax把前端的数据封装成JSON格式发送到服务器端并写成XML格式在服务器的硬盘上

    1.首先要在前端把要发送的东西(这里是一个实例化的car对象)都准备好,利用Ajax发送到服务器端,代码如下: <html xmlns="http://www.w3.org/1999/ ...

  7. Java 如何解析由String类型拼接的XML格式

    String xml = new String(a);打印的xml 的值是 <?xml version= 1.0 encoding=gb2312?><weighData>< ...

  8. java解析Xml格式的字符串

    最近在工作中,需要调别的接口,接口返回的是一个字符串,而且内容是xml格式的,结果在解析json的时候报错,最终修改了接口的返回方式,以Map返回, 才得以接收到这个xml的字符串,然后通过dom4j ...

  9. JAVA实现map集合转Xml格式

    import java.util.Iterator; import java.util.SortedMap; import java.util.TreeMap; public class MainTe ...

随机推荐

  1. h5的api dom全屏展示

    下面是完整的例子,暂不做分析 <!DOCTYPE html> <html> <head> <title> FullScreen API 演示</t ...

  2. 一周学会Mootools 1.4中文教程:(1)Dom选择器

    利器: 君欲善其事须先利其器,好吧因为我们的时间比较紧迫,只有六天而已,那么六天的时间用死记硬背的方式学会Mt犹如天方夜谭,因此我们需要借鉴一下Editplus的素材栏帮我们记忆就好了,当我们需要用到 ...

  3. 【LeetCode题意分析&解答】39. Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  4. [LeetCode]题解(python):143-Reorder List

    题目来源: https://leetcode.com/problems/reorder-list/ 题意分析: 给定一个链表L:L0→L1→…→Ln-1→Ln,改变链表的排序为: L0→Ln→L1→L ...

  5. nginx filter

    server { listen 80; server_name g.zcdn.com; index index.html; location / { proxy_cache cache_go; pro ...

  6. Jsoup代码解读之一-概述

    Jsoup代码解读之一-概述 今天看到一个用python写的抽取正文的东东,美滋滋的用Java实现了一番,放到了webmagic里,然后发现Jsoup里已经有了…觉得自己各种不靠谱啊!算了,静下心来学 ...

  7. SQL存储过程动态查询数据区间

    以前经常看到人查询数据库采用left join及case方式,一条一条的枚举查询整个数据的数据区间方法可行,但是数据一但很大,枚举就死悄悄,在网上查看,几乎全是照抄case ,left join枚举无 ...

  8. Oracle闪回操作

    Oracle闪回操作 1. 记录当前时间或SCN 在数据库变动前记录时间或SCN SQL> select  to_char(sysdate,'YYYY-MM-DD HH24:mi:ss') fr ...

  9. 对JAVA Bean使用PropertyDescriptor反射调用JAVA方法

    对于符合JAVA Bean规范的bean,调用其方法应优先使用java.beans.PropertyDescriptor获取Method进行方法调用,以获得更大的可维护性. 示比例如以下: NonCo ...

  10. c++中string::function集合

    string append() 1.直接添加另一个完整的字符串: str1.append(str2); 2.添加另一个字符串的某一段字串:    str1.append(str2, 11,  7); ...