java发送http请求,内容为xml格式&&传统URI类请求
- 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();
- }
- }
java发送http请求,内容为xml格式&&传统URI类请求的更多相关文章
- 对于java用发送http请求,请求内容为xml格式
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStr ...
- Java发HTTP POST请求(内容为xml格式)
Java发HTTP POST请求(内容为xml格式) 一.POST请求 服务器地址:http://5.0.217.50:17001/VideoSend 服务器提供的是xml格式的http接口,接口定义 ...
- ajax数据请求4(xml格式)
ajax数据请求4(xml格式): <!doctype html> <html> <head> <meta charset="utf-8" ...
- python接口自动化(四十一)- 发xml格式参数的post请求(超详解)
简介 最近在工作中,遇到一种奇葩的接口,它的参数数据是通过xml,进行传递的,不要大惊小怪的,林子大了什么鸟都有,每个人的思路想法不一样,开发的接口也是各式各样的,如果想要统一的话,必须是提前团队已经 ...
- Java发送HTTP POST请求(内容为xml格式)
今天在给平台用户提供http简单接口的时候,顺便写了个调用的Java类供他参考. 服务器地址:http://5.0.217.50:17001/VideoSend 服务器提供的是xml格式的h ...
- Java POI 读取Excel数据转换为XML格式
1.首先要下载poi相关的包:http://poi.apache.org/ ,以下是所需的jar包 2.贴上详细的代码 public class ExcelToXml { /** * 将excel的 ...
- 将Java对象序列化成JSON和XML格式
1.先定义一个Java对象Person: public class Person { String name; int age; int number; public String getName() ...
- XML格式与实体类的转换
背景 本人头一回写博客,请大家多多关照.通过读取XML文件获取用户管理权限,其中涉及三部分: 1.XML文件的生成: 2.XML文件的读取: 3.XML文件的保存: 如何做 第一步:自己先将XML文件 ...
- Soup协议-即普通post请求,内容域xml
1.基础问题 1.1 soup-Simple Object Access Protocal简单对象访问协议 a).承载在http协议之上,http支持传输img/html/文件等,soup请求和响应域 ...
随机推荐
- numpy 小示例
import numpy as np 生成 3*4 的由 0 组成的二维数组 >>> np.zeros((3,4)) array([[0., 0., 0., 0.], [0., 0 ...
- 数据结构(C语言)关于查找与排序
1)利用readData()函数从data1.txt中读入不同规模的数据存入数组,编写基于数组的顺序查找算法,测试数据量为1万.5万.10万.20万.30万.40万和50万时的数据查询时间. 算法代码 ...
- Maven中使用Jetty容器
1.在pom.xml中添加Jetty的插件 <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId ...
- python查看文件的编码格式
pip install chardet 执行 import chardet f = open('a.doc',r) data = f.read() print chardet.detect(data) ...
- zabbix监控系统的配置
一.zabbix介绍 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系 ...
- JavaScript事件漫谈
内容概要: event对象,事件在多个浏览器中的兼容,事件的传播机制,JS自定义事件,jQuery的自定义事件的绑定与触发 Event对象 Event对象属于HTML DOM对象.Event 对象代表 ...
- php 给对象动态增加属性 及子类继承父类的构造方法
<?php error_reporting(-1); ini_set('display_errors','on'); class A { public $a = 'hello'; public ...
- 了解WCF的前世今生之实现服务端(一)
http://www.cnblogs.com/jiagoushi/archive/2013/03/15/2962351.html 1.WCF是对现有的分布式通信技术的一个整合,其中包括Com/DCom ...
- magento优化
magento -- SEO优化继续往前走一步 magento的SEO做的实在太好了,让其它的电子商务平台与之相比实在是不是一个重级的,这也许就是magento成功的原因.尽管现在结合apache,实 ...
- 清除的通用样式 css
/*公共样式--开始*/ html, body, div, ul, li, h1, h2, h3, h4, h5, h6, p, dl, dt, dd, ol, form, input, textar ...