对于java用发送http请求,请求内容为xml格式
- 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);
- //设置请求超时时间
- *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());
- ];
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- ;
- ){
- , count);
- }
- byte[] strByte = bos.toByteArray();
- ,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格式的更多相关文章
- Java发HTTP POST请求(内容为xml格式)
Java发HTTP POST请求(内容为xml格式) 一.POST请求 服务器地址:http://5.0.217.50:17001/VideoSend 服务器提供的是xml格式的http接口,接口定义 ...
- Java发送HTTP POST请求(内容为xml格式)
今天在给平台用户提供http简单接口的时候,顺便写了个调用的Java类供他参考. 服务器地址:http://5.0.217.50:17001/VideoSend 服务器提供的是xml格式的h ...
- 如何设置Fiddler来拦截Java代码发送的HTTP请求,进行各种问题排查
我们使用Java的RestTemplate或者Apache的HTTPClient编程的时候,经常遇到需要跟踪Java 代码发送的HTTP请求明细的情况.和javascript代码在浏览器里发送请求可以 ...
- java发送http请求,内容为xml格式&&传统URI类请求
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStr ...
- 提交post请求,参数为xml格式
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...
- 利用Ajax把前端的数据封装成JSON格式发送到服务器端并写成XML格式在服务器的硬盘上
1.首先要在前端把要发送的东西(这里是一个实例化的car对象)都准备好,利用Ajax发送到服务器端,代码如下: <html xmlns="http://www.w3.org/1999/ ...
- Java 如何解析由String类型拼接的XML格式
String xml = new String(a);打印的xml 的值是 <?xml version= 1.0 encoding=gb2312?><weighData>< ...
- java解析Xml格式的字符串
最近在工作中,需要调别的接口,接口返回的是一个字符串,而且内容是xml格式的,结果在解析json的时候报错,最终修改了接口的返回方式,以Map返回, 才得以接收到这个xml的字符串,然后通过dom4j ...
- JAVA实现map集合转Xml格式
import java.util.Iterator; import java.util.SortedMap; import java.util.TreeMap; public class MainTe ...
随机推荐
- h5的api dom全屏展示
下面是完整的例子,暂不做分析 <!DOCTYPE html> <html> <head> <title> FullScreen API 演示</t ...
- 一周学会Mootools 1.4中文教程:(1)Dom选择器
利器: 君欲善其事须先利其器,好吧因为我们的时间比较紧迫,只有六天而已,那么六天的时间用死记硬背的方式学会Mt犹如天方夜谭,因此我们需要借鉴一下Editplus的素材栏帮我们记忆就好了,当我们需要用到 ...
- 【LeetCode题意分析&解答】39. Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- [LeetCode]题解(python):143-Reorder List
题目来源: https://leetcode.com/problems/reorder-list/ 题意分析: 给定一个链表L:L0→L1→…→Ln-1→Ln,改变链表的排序为: L0→Ln→L1→L ...
- nginx filter
server { listen 80; server_name g.zcdn.com; index index.html; location / { proxy_cache cache_go; pro ...
- Jsoup代码解读之一-概述
Jsoup代码解读之一-概述 今天看到一个用python写的抽取正文的东东,美滋滋的用Java实现了一番,放到了webmagic里,然后发现Jsoup里已经有了…觉得自己各种不靠谱啊!算了,静下心来学 ...
- SQL存储过程动态查询数据区间
以前经常看到人查询数据库采用left join及case方式,一条一条的枚举查询整个数据的数据区间方法可行,但是数据一但很大,枚举就死悄悄,在网上查看,几乎全是照抄case ,left join枚举无 ...
- Oracle闪回操作
Oracle闪回操作 1. 记录当前时间或SCN 在数据库变动前记录时间或SCN SQL> select to_char(sysdate,'YYYY-MM-DD HH24:mi:ss') fr ...
- 对JAVA Bean使用PropertyDescriptor反射调用JAVA方法
对于符合JAVA Bean规范的bean,调用其方法应优先使用java.beans.PropertyDescriptor获取Method进行方法调用,以获得更大的可维护性. 示比例如以下: NonCo ...
- c++中string::function集合
string append() 1.直接添加另一个完整的字符串: str1.append(str2); 2.添加另一个字符串的某一段字串: str1.append(str2, 11, 7); ...