使用HttpClient 以POST的形式发送json字符串 
步骤: 
1.url 、parameters 
2.创建httpClient对象 
3.创建HttpPost对象 
4.为post对象设置参数 
5.将参数以实体的实行放入post对象中 
6.client.execute(post);返回response对象 
7.通过response对象获取响应码, 
8.获得结果实体

    1. public void HttpClientPostByJson(){
    2. String url = "http://localhost:8080/../../";
    3. String param = "{\"endTxnDate\":\"20151114\",\"merchId\":\"\",\"sendTime\":\"201510132012123\",\"startTxnDate\":\"20151111\"}";
    4. //创建client和post对象
    5. HttpClient client = HttpClients.createDefault();
    6. HttpPost post = new HttpPost(url);
    7. //json形式
    8. post.addHeader("content-type", "application/json;charset=utf-8");
    9. post.addHeader("accept","application/json");
    10. //json字符串以实体的实行放到post中
    11. post.setEntity(new StringEntity(param,Charset.forName("utf-8")));
    12. HttpResponse response = null;
    13. try {
    14. //获得response对象
    15. response = client.execute(post);
    16. } catch (Exception e) {
    17. e.printStackTrace();
    18. }
    19. if(HttpStatus.SC_OK!=response.getStatusLine().getStatusCode()){
    20. System.out.println("请求返回不正确");
    21. }
    22. String result="";
    23. try {
    24. //获得字符串形式的结果
    25. result = EntityUtils.toString(response.getEntity());
    26. } catch (Exception e) {
    27. e.printStackTrace();
    28. }
    29. System.out.println(result);
    30. }

最新文章

  1. 工业串口和网络软件通讯平台(SuperIO 2.0)发布
  2. 【leetcode】Search in Rotated Sorted Array (hard)
  3. MySQL慢日志监控脚本实例剖析
  4. CSS3实现气泡效果
  5. Error 2147943712 during task creation
  6. SO_REUSEADDR
  7. Spring 使用注解方式进行事物管理
  8. ubuntu下lnmp的安装
  9. 说说Xcode4中xib绑定的原理
  10. CodeForces 543A - Writing Code DP 完全背包
  11. Jquery右击显示菜单事件,运用smartMenu插件。
  12. PHP简单分页省略中间页码
  13. 【Webpack的使用指南 01】Webpack入门
  14. electron 热更新
  15. redis 系列13 集合对象
  16. java中对象的创建过程
  17. Mysql安装、设置密码、编码
  18. linux下nginx日常操作
  19. HOWTO: 如何利用Avizo或Amira计算孔隙率(Porosity)
  20. 最大公约数&&最小公倍数

热门文章

  1. html css javascript实现弹弹球
  2. Java实现 LeetCode 509 斐波那契数
  3. Java蓝桥杯 算法训练 复数归一化
  4. Java实现 蓝桥杯VIP 算法训练 P1101
  5. Java实现 LeetCode 162 寻找峰值
  6. CUDA优化
  7. linux基础相关命令
  8. .NET Core SDKs installed: No SDKs were found.
  9. 安装apoc插件
  10. PBFT共识算法