博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用JAVA发送一个XML格式的HTTP请求
阅读量:6977 次
发布时间:2019-06-27

本文共 6411 字,大约阅读时间需要 21 分钟。

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("
"); 138 sb.append("
"); 139 sb.append("
"); 140 sb.append("
readMeetingStatus
"); 141 sb.append("
meeting
"); 142 sb.append("
xml
"); 143 sb.append("
admin
"); 144 sb.append("
admin
"); 145 sb.append("
box
"); 146 sb.append("
"); 147 sb.append(" "); 148 sb.append("
43283344
"); 149 sb.append(" "); 150 sb.append("
"); 151 152 return sb.toString(); 153 } 154 }

 

转载地址:http://teupl.baihongyu.com/

你可能感兴趣的文章
安装sql 2005 闪退 连接到服务器失败。错误:0x80070424
查看>>
Linux下分割与合并文件的方法
查看>>
安全攻防实战:使用winlogonhack获取系统密码
查看>>
AD RMS高可用(三)部署RMS根群集服务器
查看>>
5.1 python的缩进
查看>>
瀚思首发三款产品 推动大数据安全战略布局
查看>>
全“芯”关注用户需求 AMD“超轻薄笔记本”杀出重围
查看>>
自定义通知与系统通知的学习(详解)
查看>>
软考新思维--2017年上半年信息系统项目管理师上午试题分析与答案(试题16-20题)...
查看>>
C#编码简单性之语义篇(如何编写简短的C#代码,随时更新)
查看>>
使用sudo进入root权限,以及防止root密码被恶意篡改
查看>>
Android 多媒体综述
查看>>
route命令相关整理
查看>>
关于VS2012如何安装Windows Phone Toolkit
查看>>
Forefront for OCS2007之部署
查看>>
IBM Thinkpad T43-44U 升级到 2G 内存后少了 66M
查看>>
【No.1_sizeof与strlen】
查看>>
SMO学习笔记(二)——还原(恢复)篇之完整恢复
查看>>
Windows Server 2003 AD升级到Windows Server 2008 AD的方法及详细步骤
查看>>
设置php-fpm使用socket文件
查看>>