post 方式提交XML文件调用接口
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.HttpStatus;
- import org.apache.commons.httpclient.methods.PostMethod;
- import org.apache.commons.httpclient.methods.RequestEntity;
- import org.apache.commons.httpclient.methods.StringRequestEntity;
- public class Test {
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception{
- //直接字符串拼接
- StringBuffer sb = new StringBuffer();
- sb.append("<app_ei_sync_req><enabler_id>pengxwtest</enabler_id><dev_id>pengxwtest</dev_id>" +
- "<app_id>pengxwtest</app_id><app_secret>pengxwtest</app_secret>" +
- "<app_status>2</app_status><app_level>0</app_level><app_ei><ei_id>1</ei_id>" +
- "<ei_id>2</ei_id><ei_id>3</ei_id></app_ei></app_ei_sync_req>");//xml数据存储
- String data = sb.toString();
- String url = "接口地址";
- HttpClient httpclient = new HttpClient();
- PostMethod post = new PostMethod(url);
- String info = null;
- try {
- RequestEntity entity = new StringRequestEntity(data, "text/xml",
- "iso-8859-1");
- post.setRequestEntity(entity);
- httpclient.executeMethod(post);
- int code = post.getStatusCode();
- if (code == HttpStatus.SC_OK)
- info = new String(post.getResponseBodyAsString()); //接口返回的信息
- } catch (Exception ex) {
- ex.printStackTrace();
- } finally {
- post.releaseConnection();
- }
- System.out.println(info);
- }
- //读取xml文件
- public class xmlTool(){
- InputStreamReader read = new InputStreamReader (new FileInputStream("f://aa.xml"),"UTF-8");
- StringBuffer sb = new StringBuffer();
- BufferedReader br = new BufferedReader(read);
- String row;
- while((row = br.readLine())!=null){
- sb.append(row.trim());
- }
- String data = sb.toString();
- String url = "http://localhost:9099/vtoss/cloudapi/rp_video_transcode_batch.do";
- HttpClient httpclient = new HttpClient();
- PostMethod post = new PostMethod(url);
- String info = null;
- try {
- RequestEntity entity = new StringRequestEntity(data, "text/xml",
- "UTF-8");
- post.setRequestEntity(entity);
- httpclient.executeMethod(post);
- int code = post.getStatusCode();
- if (code == HttpStatus.SC_OK)
- info = new String(post.getResponseBodyAsString());
- } catch (Exception ex) {
- ex.printStackTrace();
- } finally {
- post.releaseConnection();
- }
- System.out.println(info);
- }
- }
转向的处理
- private void postMethod(String url) throws IOException
- {
- url = "http://www.newsmth.net/bbslogin2.php";
- PostMethod postMethod = new PostMethod(url);
- // 填入各个表单域的值
- NameValuePair[] data = { new NameValuePair("id", "herrapfel"),new NameValuePair("passwd", "") };
- // 将表单的值放入postMethod中
- postMethod.setRequestBody(data);
- // 执行postMethod
- int statusCode = httpClient.executeMethod(postMethod);
- System.out.println(" status code:" + statusCode);
- // HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
- if(statusCode == HttpStatus.SC_OK)
- {
- StringBuffer contentBuffer = new StringBuffer();
- InputStream in = postMethod.getResponseBodyAsStream();
- BufferedReader reader = new BufferedReader(new InputStreamReader(in,postMethod.getResponseCharSet()));
- String inputLine = null;
- while((inputLine = reader.readLine()) != null)
- {
- contentBuffer.append(inputLine);
- System.out.println("input line:"+ inputLine);
- contentBuffer.append("/n");
- }
- in.close();
- }
- else if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
- {
- // 从头中取出转向的地址
- Header locationHeader = postMethod.getResponseHeader("location");
- String location = null;
- if (locationHeader != null)
- {
- location = locationHeader.getValue();
- System.out.println("The page was redirected to:" + location);
- }
- else
- {
- System.err.println("Location field value is null.");
- }
- }
- }
从文件读取
- import java.io.File;
- import java.io.FileInputStream;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
- import org.apache.commons.httpclient.methods.PostMethod;
- /**
- * 用来演示提交XML格式数据的例子
- */
- public class PostXMLClient {
- public static void main(String[] args) throws Exception {
- File input = new File(“test.xml”);
- PostMethod post = new PostMethod(“http://localhost:8080/httpclient/xml.jsp”);
- // 设置请求的内容直接从文件中读取
- post.setRequestBody(new FileInputStream(input));
- if (input.length() < Integer.MAX_VALUE)
- post.setRequestContentLength(input.length());
- else
- post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
- // 指定请求内容的类型
- post.setRequestHeader("Content-type", "text/xml; charset=GBK");
- HttpClient httpclient = new HttpClient();
- int result = httpclient.executeMethod(post);
- System.out.println("Response status code: " + result);
- System.out.println("Response body: ");
- System.out.println(post.getResponseBodyAsString());
- post.releaseConnection();
- }
- }
post 方式提交XML文件调用接口的更多相关文章
- 通俗易懂,C#如何安全、高效地玩转任何种类的内存之Span的脾气秉性(二)。 异步委托 微信小程序支付证书及SSL证书使用 SqlServer无备份下误删数据恢复 把list集合的内容写入到Xml中,通过XmlDocument方式写入Xml文件中 通过XDocument方式把List写入Xml文件
通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的脾气秉性(二). 前言 读完上篇<通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的本质(一).>,相信大家对sp ...
- 用JAXP的dom方式解析XML文件
用JAXP的dom方式解析XML文件,实现增删改查操作 dom方式解析XML原理 XML文件 <?xml version="1.0" encoding="UTF-8 ...
- ajax方式提交带文件上传的表单,上传后不跳转
ajax方式提交带文件上传的表单 一般的表单都是通过ajax方式提交,所以碰到带文件上传的表单就比较麻烦.基本原理就是在页面增加一个隐藏iframe,然后通过ajax提交除文件之外的表单数据,在表单数 ...
- PHP 以POST方式提交XML、获取XML,最后解析XML
以POST方式提交XML // Do a POST $data="<?xml version='1.0' encoding='UTF-8'?> <TypeRsp> & ...
- 在iOS 开发中用GDataXML(DOM方式)解析xml文件
因为GDataXML的内部实现是通过DOM方式解析的,而在iOS 开发中用DOM方式解析xml文件,这个时候我们需要开启DOM,因为ios 开发中是不会自动开启的,只有在mac 开发中才自动开启的.我 ...
- Java&Xml教程(二)使用DOM方式解析XML文件
DOM XML 解析方式是最容易理解的,它將XML文件作为Document对象读取到内存中,然后你可以轻松地遍历不同的元素和节点对象.遍历元素和节点不需要按照顺序进行. DOM解析方式适合尺寸较小的X ...
- 如何在Mybatis的xml文件调用java类的方法
在mybatis的映射xml文件调用java类的方法:使用的是OGNL表达式,表达式格式为:${@prefix@methodName(传递参数名称)} 1.如下代码所示:方法必须为静态方法:以下我只是 ...
- Java&Xml教程(五)使用SAX方式解析XML文件
Java SAX解析机制为我们提供了一系列的API来处理XML文件,SAX解析和DOM解析方式不太一样,它并不是將XML文件内容一次性全部加载,而是连续的部分加载. javax.xml.parsers ...
- asp.net使用wsdl文件调用接口,以及调用SSL接口报错“根据验证过程 远程证书无效”的处理
1.调用wsdl接口,首先需要将wsdl文件转换为cs文件: 进入VS 开发人员命令提示行,输入如下命令: c:/Program Files/Microsoft Visual Studio 8/VC& ...
随机推荐
- Oracle Database Concepts:介绍模式对象(Introduction to Schema Objects)
数据库模式(schema)是数据结构的逻辑容器,被称作模式对象(schema objects) 每一个数据库用户拥有一个和用户名相同的模式,例如hr用户拥有hr模式. 在一个产品数据库中,模式的拥有者 ...
- equals方法,hashcode()方法
Object类的equals 方法 用来检测两个对象是否相等,即两个对象的内容是否相等,区分大小写. (一)说到equals方法,不得不提一下==号. ==用于比较引用和比较原生数据类型时具有不同 ...
- hdu1007
Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some ...
- AppCan教你从零开始做开发
经常收到类似这样的提问:新手开发APP,要怎么学?我有满屏幕的文档和视频,然而并没有什么卵用,因为我不知道该从哪看起……今天的主要内容是教大家,如何在AppCan移动平台创建应用,引擎插件选择.证书管 ...
- Android--将图片存放到我们本地
代码里面有详细的解释,我就不多说了 //处理并保存图像 private File dealPhoto(Bitmap photo){ FileOutputStream fileOutputStream ...
- hdu 5273 Dylans loves sequence
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5273 Dylans loves sequence Description Dylans is give ...
- sql server 查询数据库所有的表名+字段
SELECT * FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='Account' SELECT (case when a.colorder= ...
- TWaver初学实战——基于HTML5的交互式地铁图
每天坐地铁,经常看地铁图,有一天突然想到,地铁图不也是一种拓扑结果吗?TWaver到底能与地铁图擦出怎样的火花呢? 想到就干,先到网上找幅参考图.各种风格的地铁图还挺多,甚至有大学生自主设计制作, ...
- PostgreSQL中的引号和null
今天工作时写了一个sql,但是PostgreSQL总是提示有语法错误,简单的做个记录: 问题1.'' 和“”单引号和双引号 举个例子: #.select id, delivery_d ...
- 网页数据采集 - 系列之Flash数据采集
经常看到一些朋友在讨论如何采集flash中的数据,讨论来讨论区,结论就是:flash不能采集,其实也不总是这样.本篇就跟大家分享如何采集flash中的数据. 在开始之前,先说明一下:一般来说flash ...