对于如何调用第三方接口还是有些模糊,所以记录一下,上代码

    1. package com.zhang.miaodou;

      import java.io.BufferedReader;
      import java.io.DataOutputStream;
      import java.io.InputStreamReader;
      import java.net.HttpURLConnection;
      import java.net.URL;
      import java.net.URLEncoder; public class DemoTest1 { public static final String GET_URL = "http://112.4.27.9/mall-back/if_user/store_list?storeId=32"; // public static final String POST_URL = "http://112.4.27.9/mall-back/if_user/store_list";
      // 妙兜测试接口
      public static final String POST_URL = "http://121.40.204.191:8180/mdserver/service/installLock"; /** * 接口调用 GET */ public static void httpURLConectionGET() {
      try { URL url = new URL(GET_URL); // 把字符串转换为URL请求地址 HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开连接 connection.connect();// 连接会话 // 获取输入流 BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String line; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) {// 循环读取流 sb.append(line); } br.close();// 关闭流 connection.disconnect();// 断开连接 System.out.println(sb.toString()); } catch (Exception e) { e.printStackTrace(); System.out.println("失败!"); } } /** * 接口调用 POST */ public static void httpURLConnectionPOST () { try { URL url = new URL(POST_URL);
      // 将url 以 open方法返回的urlConnection 连接强转为HttpURLConnection连接 (标识一个url所引用的远程对象连接)
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 此时cnnection只是为一个连接对象,待连接中
      // 设置连接输出流为true,默认false (post 请求是以流的方式隐式的传递参数)
      connection.setDoOutput(true);
      // 设置连接输入流为true
      connection.setDoInput(true);
      // 设置请求方式为post
      connection.setRequestMethod("POST");
      // post请求缓存设为false
      connection.setUseCaches(false);
      // 设置该HttpURLConnection实例是否自动执行重定向
      connection.setInstanceFollowRedirects(true);
      // 设置请求头里面的各个属性 (以下为设置内容的类型,设置为经过urlEncoded编码过的from参数)
      // application/x-javascript text/xml->xml数据 application/x-javascript->json对象 application/x-www-form-urlencoded->表单数据
      // ;charset=utf-8 必须要,不然妙兜那边会出现乱码【★★★★★】
      connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
      // 建立连接 (请求未开始,直到connection.getInputStream()方法调用时才发起,以上各个参数设置需在此方法之前进行)
      connection.connect();
      // 创建输入输出流,用于往连接里面输出携带的参数,(输出内容为?后面的内容)
      DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
      String app_key = "app_key="+ URLEncoder.encode("4f7bf8c8260124e6e9c6bf094951a111", "utf-8"); // 已修改【改为错误数据,以免信息泄露】
      String agt_num = "&agt_num="+ URLEncoder.encode("10111", "utf-8"); // 已修改【改为错误数据,以免信息泄露】
      String pid = "&pid="+ URLEncoder.encode("BLZXA150401111", "utf-8"); // 已修改【改为错误数据,以免信息泄露】
      String departid = "&departid="+ URLEncoder.encode("10007111", "utf-8"); // 已修改【改为错误数据,以免信息泄露】
      String install_lock_name = "&install_lock_name="+ URLEncoder.encode("南天大门", "utf-8");
      String install_address = "&install_address="+ URLEncoder.encode("北京育新", "utf-8");
      String install_gps = "&install_gps="+ URLEncoder.encode("116.350888,40.011001", "utf-8");
      String install_work = "&install_work="+ URLEncoder.encode("小李", "utf-8");
      String install_telete = "&install_telete="+ URLEncoder.encode("13000000000", "utf-8");
      String intall_comm = "&intall_comm="+ URLEncoder.encode("一切正常", "utf-8"); // 格式 parm = aaa=111&bbb=222&ccc=333&ddd=444
      String parm = app_key+ agt_num+ pid+ departid+ install_lock_name+ install_address+ install_gps+ install_work+ install_telete+ intall_comm;
      // 将参数输出到连接
      dataout.writeBytes(parm);
      // 输出完成后刷新并关闭流
      dataout.flush();
      dataout.close(); // 重要且易忽略步骤 (关闭流,切记!)
      // System.out.println(connection.getResponseCode());
      // 连接发起请求,处理服务器响应 (从连接获取到输入流并包装为bufferedReader) BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String line; StringBuilder sb = new StringBuilder(); // 用来存储响应数据 // 循环读取流,若不到结尾处 while ((line = bf.readLine()) != null) { // sb.append(bf.readLine()); sb.append(line).append(System.getProperty("line.separator")); }
      bf.close(); // 重要且易忽略步骤 (关闭流,切记!) connection.disconnect(); // 销毁连接 System.out.println(sb.toString()); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { // httpURLConectionGET(); httpURLConnectionPOST(); }
      } 

      只使用了POST请求方法,GET没有用,为了保证代码完整性所以没有删除GET请求代码 
          返回结果:

      {
      "status" : "fail", "code" : "ERR001", "msg" : "商户10111不存在" }

        转载于:https://blog.csdn.net/Javaming_o_O/article/details/91411649

最新文章

  1. 前端开发自学之JavaScript——显示当前时间
  2. Laravel学习笔记(三)数据库 数据库迁移
  3. 扩展GridView控件——为内容项添加拖放及分组功能
  4. CentOS7 mono环境连接WCF
  5. (原创)openvswitch实验连载2-cisco模拟器IOU-Web安装及网络环境配置
  6. 插入ts以及判断列是否存在(支持多数据库)
  7. 2015第10周四-CSS小结
  8. 【具体数学--读书笔记】1.1 The Power of Hanoi
  9. ThinkPHP - 登录流程
  10. Mac下node.js卸载方法收集
  11. jquery、js获取页面高度宽度等
  12. go 监听系统信号
  13. js-变量定义关键字const,var,let
  14. 读取 android sys/下的信息
  15. Git GUI中文乱码问题解决方法
  16. mock数据和代码生成
  17. Visual Studio 项目模板制作(四)
  18. winrar 命令行 解压文件
  19. 处理tomcat内存溢出问题
  20. javascript总结34 :DOM之节点元素获取

热门文章

  1. 【机器学习】李宏毅——生成式对抗网络GAN
  2. nacos注册中心单节点ap架构源码解析
  3. .Net引用根目录子文件夹下的dll文件
  4. 第七节 VOR/DME进近程序保护区的绘制
  5. C++进阶(智能指针)
  6. python进阶之路19 地狱之门购物车!!!!
  7. 音频音量调整中的ramp up & down
  8. Codeforces Round #844 (Div.1 + Div.2) CF 1782 A~F 题解
  9. 小项目中vuex使用频率不太多我们完全可以用provide inject 来代替可以让项目小不少
  10. C Primer Plus(4.8)編程練習