1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.List;
  6.  
  7. import org.apache.commons.httpclient.HttpClient;
  8. import org.apache.commons.httpclient.HttpStatus;
  9. import org.apache.commons.httpclient.methods.PostMethod;
  10. import org.apache.commons.httpclient.methods.RequestEntity;
  11. import org.apache.commons.httpclient.methods.StringRequestEntity;
  12.  
  13. public class Test {
  14.  
  15. /**
  16. * @param args
  17. */
  18. public static void main(String[] args) throws Exception{
  19.  
  20. //直接字符串拼接
  21. StringBuffer sb = new StringBuffer();
  22. sb.append("<app_ei_sync_req><enabler_id>pengxwtest</enabler_id><dev_id>pengxwtest</dev_id>" +
  23. "<app_id>pengxwtest</app_id><app_secret>pengxwtest</app_secret>" +
  24. "<app_status>2</app_status><app_level>0</app_level><app_ei><ei_id>1</ei_id>" +
  25. "<ei_id>2</ei_id><ei_id>3</ei_id></app_ei></app_ei_sync_req>");//xml数据存储
  26. String data = sb.toString();
  27. String url = "接口地址";
  28. HttpClient httpclient = new HttpClient();
  29. PostMethod post = new PostMethod(url);
  30. String info = null;
  31. try {
  32. RequestEntity entity = new StringRequestEntity(data, "text/xml",
  33. "iso-8859-1");
  34. post.setRequestEntity(entity);
  35. httpclient.executeMethod(post);
  36. int code = post.getStatusCode();
  37. if (code == HttpStatus.SC_OK)
  38. info = new String(post.getResponseBodyAsString()); //接口返回的信息
  39. } catch (Exception ex) {
  40. ex.printStackTrace();
  41. } finally {
  42. post.releaseConnection();
  43. }
  44. System.out.println(info);
  45.  
  46. }
  47.  
  48. //读取xml文件
  49.  
  50. public class xmlTool(){
  51.  
  52. InputStreamReader read = new InputStreamReader (new FileInputStream("f://aa.xml"),"UTF-8");
  53.  
  54. StringBuffer sb = new StringBuffer();
  55.  
  56. BufferedReader br = new BufferedReader(read);
  57. String row;
  58. while((row = br.readLine())!=null){
  59. sb.append(row.trim());
  60.  
  61. }
  62. String data = sb.toString();
  63. String url = "http://localhost:9099/vtoss/cloudapi/rp_video_transcode_batch.do";
  64. HttpClient httpclient = new HttpClient();
  65. PostMethod post = new PostMethod(url);
  66. String info = null;
  67. try {
  68. RequestEntity entity = new StringRequestEntity(data, "text/xml",
  69. "UTF-8");
  70. post.setRequestEntity(entity);
  71. httpclient.executeMethod(post);
  72. int code = post.getStatusCode();
  73. if (code == HttpStatus.SC_OK)
  74. info = new String(post.getResponseBodyAsString());
  75. } catch (Exception ex) {
  76. ex.printStackTrace();
  77. } finally {
  78. post.releaseConnection();
  79. }
  80. System.out.println(info);
  81.  
  82. }
  83. }

 转向的处理

  1. private void postMethod(String url) throws IOException
  2. {
  3. url = "http://www.newsmth.net/bbslogin2.php";
  4. PostMethod postMethod = new PostMethod(url);
  5. // 填入各个表单域的值
  6. NameValuePair[] data = { new NameValuePair("id", "herrapfel"),new NameValuePair("passwd", "") };
  7. // 将表单的值放入postMethod中
  8. postMethod.setRequestBody(data);
  9. // 执行postMethod
  10. int statusCode = httpClient.executeMethod(postMethod);
  11. System.out.println(" status code:" + statusCode);
  12. // HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
  13.  
  14. if(statusCode == HttpStatus.SC_OK)
  15. {
  16. StringBuffer contentBuffer = new StringBuffer();
  17. InputStream in = postMethod.getResponseBodyAsStream();
  18. BufferedReader reader = new BufferedReader(new InputStreamReader(in,postMethod.getResponseCharSet()));
  19. String inputLine = null;
  20. while((inputLine = reader.readLine()) != null)
  21. {
  22. contentBuffer.append(inputLine);
  23. System.out.println("input line:"+ inputLine);
  24. contentBuffer.append("/n");
  25. }
  26. in.close();
  27.  
  28. }
  29. else if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
  30. {
  31. // 从头中取出转向的地址
  32. Header locationHeader = postMethod.getResponseHeader("location");
  33. String location = null;
  34. if (locationHeader != null)
  35. {
  36. location = locationHeader.getValue();
  37. System.out.println("The page was redirected to:" + location);
  38. }
  39. else
  40. {
  41. System.err.println("Location field value is null.");
  42. }
  43. }
  44.  
  45. }

从文件读取

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import org.apache.commons.httpclient.HttpClient;
  4. import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
  5. import org.apache.commons.httpclient.methods.PostMethod;
  6. /**
  7. * 用来演示提交XML格式数据的例子
  8. */
  9. public class PostXMLClient {
  10. public static void main(String[] args) throws Exception {
  11. File input = new File(“test.xml”);
  12. PostMethod post = new PostMethod(“http://localhost:8080/httpclient/xml.jsp”);
  13. // 设置请求的内容直接从文件中读取
  14. post.setRequestBody(new FileInputStream(input));
  15. if (input.length() < Integer.MAX_VALUE)
  16. post.setRequestContentLength(input.length());
  17. else
  18. post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
  19. // 指定请求内容的类型
  20. post.setRequestHeader("Content-type", "text/xml; charset=GBK");
  21. HttpClient httpclient = new HttpClient();
  22. int result = httpclient.executeMethod(post);
  23. System.out.println("Response status code: " + result);
  24. System.out.println("Response body: ");
  25. System.out.println(post.getResponseBodyAsString());
  26. post.releaseConnection();
  27. }
  28. }

post 方式提交XML文件调用接口的更多相关文章

  1. 通俗易懂,C#如何安全、高效地玩转任何种类的内存之Span的脾气秉性(二)。 异步委托 微信小程序支付证书及SSL证书使用 SqlServer无备份下误删数据恢复 把list集合的内容写入到Xml中,通过XmlDocument方式写入Xml文件中 通过XDocument方式把List写入Xml文件

    通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的脾气秉性(二).   前言 读完上篇<通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的本质(一).>,相信大家对sp ...

  2. 用JAXP的dom方式解析XML文件

    用JAXP的dom方式解析XML文件,实现增删改查操作 dom方式解析XML原理 XML文件 <?xml version="1.0" encoding="UTF-8 ...

  3. ajax方式提交带文件上传的表单,上传后不跳转

    ajax方式提交带文件上传的表单 一般的表单都是通过ajax方式提交,所以碰到带文件上传的表单就比较麻烦.基本原理就是在页面增加一个隐藏iframe,然后通过ajax提交除文件之外的表单数据,在表单数 ...

  4. PHP 以POST方式提交XML、获取XML,最后解析XML

    以POST方式提交XML // Do a POST $data="<?xml version='1.0' encoding='UTF-8'?> <TypeRsp> & ...

  5. 在iOS 开发中用GDataXML(DOM方式)解析xml文件

    因为GDataXML的内部实现是通过DOM方式解析的,而在iOS 开发中用DOM方式解析xml文件,这个时候我们需要开启DOM,因为ios 开发中是不会自动开启的,只有在mac 开发中才自动开启的.我 ...

  6. Java&Xml教程(二)使用DOM方式解析XML文件

    DOM XML 解析方式是最容易理解的,它將XML文件作为Document对象读取到内存中,然后你可以轻松地遍历不同的元素和节点对象.遍历元素和节点不需要按照顺序进行. DOM解析方式适合尺寸较小的X ...

  7. 如何在Mybatis的xml文件调用java类的方法

    在mybatis的映射xml文件调用java类的方法:使用的是OGNL表达式,表达式格式为:${@prefix@methodName(传递参数名称)} 1.如下代码所示:方法必须为静态方法:以下我只是 ...

  8. Java&Xml教程(五)使用SAX方式解析XML文件

    Java SAX解析机制为我们提供了一系列的API来处理XML文件,SAX解析和DOM解析方式不太一样,它并不是將XML文件内容一次性全部加载,而是连续的部分加载. javax.xml.parsers ...

  9. asp.net使用wsdl文件调用接口,以及调用SSL接口报错“根据验证过程 远程证书无效”的处理

    1.调用wsdl接口,首先需要将wsdl文件转换为cs文件: 进入VS 开发人员命令提示行,输入如下命令: c:/Program Files/Microsoft Visual Studio 8/VC& ...

随机推荐

  1. Oracle Database Concepts:介绍模式对象(Introduction to Schema Objects)

    数据库模式(schema)是数据结构的逻辑容器,被称作模式对象(schema objects) 每一个数据库用户拥有一个和用户名相同的模式,例如hr用户拥有hr模式. 在一个产品数据库中,模式的拥有者 ...

  2. equals方法,hashcode()方法

    Object类的equals 方法 用来检测两个对象是否相等,即两个对象的内容是否相等,区分大小写.   (一)说到equals方法,不得不提一下==号. ==用于比较引用和比较原生数据类型时具有不同 ...

  3. hdu1007

    Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some ...

  4. AppCan教你从零开始做开发

    经常收到类似这样的提问:新手开发APP,要怎么学?我有满屏幕的文档和视频,然而并没有什么卵用,因为我不知道该从哪看起……今天的主要内容是教大家,如何在AppCan移动平台创建应用,引擎插件选择.证书管 ...

  5. Android--将图片存放到我们本地

    代码里面有详细的解释,我就不多说了 //处理并保存图像 private File dealPhoto(Bitmap photo){ FileOutputStream fileOutputStream ...

  6. hdu 5273 Dylans loves sequence

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5273 Dylans loves sequence Description Dylans is give ...

  7. sql server 查询数据库所有的表名+字段

    SELECT * FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='Account' SELECT    (case when a.colorder= ...

  8. TWaver初学实战——基于HTML5的交互式地铁图

    每天坐地铁,经常看地铁图,有一天突然想到,地铁图不也是一种拓扑结果吗?TWaver到底能与地铁图擦出怎样的火花呢?   想到就干,先到网上找幅参考图.各种风格的地铁图还挺多,甚至有大学生自主设计制作, ...

  9. PostgreSQL中的引号和null

    今天工作时写了一个sql,但是PostgreSQL总是提示有语法错误,简单的做个记录:  问题1.'' 和“”单引号和双引号         举个例子: #.select id, delivery_d ...

  10. 网页数据采集 - 系列之Flash数据采集

    经常看到一些朋友在讨论如何采集flash中的数据,讨论来讨论区,结论就是:flash不能采集,其实也不总是这样.本篇就跟大家分享如何采集flash中的数据. 在开始之前,先说明一下:一般来说flash ...