java调用http接口的记录

最近几天因为项目需求,做一个门禁管理。而门禁信息来源则是妙兜。所以我们这边需要调用妙兜的接口,主要是“设备安装登记接口”和“钥匙凭证发放接口”。

因为之前没有做过“java调用http接口”类似功能,所以在网上找了很多,也比较久。如下代码感觉比较可以,使用过程中也没出什么问题,所以就记录了下来。

    代码如下:

  1. package com.zhang.miaodou;
  2. import java.io.BufferedReader;
  3. import java.io.DataOutputStream;
  4. import java.io.InputStreamReader;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import java.net.URLEncoder;
  8. public class DemoTest1 {
  9. public static final String GET_URL = "http://112.4.27.9/mall-back/if_user/store_list?storeId=32";
  10. // public static final String POST_URL = "http://112.4.27.9/mall-back/if_user/store_list";
  11. // 妙兜测试接口
  12. public static final String POST_URL = "http://121.40.204.191:8180/mdserver/service/installLock";
  13. /**
  14. * 接口调用 GET
  15. */
  16. public static void httpURLConectionGET() {
  17. try {
  18. URL url = new URL(GET_URL); // 把字符串转换为URL请求地址
  19. HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 打开连接
  20. connection.connect(); // 连接会话
  21. // 获取输入流
  22. BufferedReader br = new BufferedReader( new InputStreamReader(connection.getInputStream(), "UTF-8"));
  23. String line;
  24. StringBuilder sb = new StringBuilder();
  25. while ((line = br.readLine()) != null) { // 循环读取流
  26. sb.append(line);
  27. }
  28. br.close(); // 关闭流
  29. connection.disconnect(); // 断开连接
  30. System.out.println(sb.toString());
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. System.out.println( "失败!");
  34. }
  35. }
  36. /**
  37. * 接口调用 POST
  38. */
  39. public static void httpURLConnectionPOST () {
  40. try {
  41. URL url = new URL(POST_URL);
  42. // 将url 以 open方法返回的urlConnection 连接强转为HttpURLConnection连接 (标识一个url所引用的远程对象连接)
  43. HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 此时cnnection只是为一个连接对象,待连接中
  44. // 设置连接输出流为true,默认false (post 请求是以流的方式隐式的传递参数)
  45. connection.setDoOutput( true);
  46. // 设置连接输入流为true
  47. connection.setDoInput( true);
  48. // 设置请求方式为post
  49. connection.setRequestMethod( "POST");
  50. // post请求缓存设为false
  51. connection.setUseCaches( false);
  52. // 设置该HttpURLConnection实例是否自动执行重定向
  53. connection.setInstanceFollowRedirects( true);
  54. // 设置请求头里面的各个属性 (以下为设置内容的类型,设置为经过urlEncoded编码过的from参数)
  55. // application/x-javascript text/xml->xml数据 application/x-javascript->json对象 application/x-www-form-urlencoded->表单数据
  56. // ;charset=utf-8 必须要,不然妙兜那边会出现乱码【★★★★★】
  57. connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
  58. // 建立连接 (请求未开始,直到connection.getInputStream()方法调用时才发起,以上各个参数设置需在此方法之前进行)
  59. connection.connect();
  60. // 创建输入输出流,用于往连接里面输出携带的参数,(输出内容为?后面的内容)
  61. DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
  62. String app_key = "app_key="+ URLEncoder.encode( "4f7bf8c8260124e6e9c6bf094951a111", "utf-8"); // 已修改【改为错误数据,以免信息泄露】
  63. String agt_num = "&agt_num="+ URLEncoder.encode( "10111", "utf-8"); // 已修改【改为错误数据,以免信息泄露】
  64. String pid = "&pid="+ URLEncoder.encode( "BLZXA150401111", "utf-8"); // 已修改【改为错误数据,以免信息泄露】
  65. String departid = "&departid="+ URLEncoder.encode( "10007111", "utf-8"); // 已修改【改为错误数据,以免信息泄露】
  66. String install_lock_name = "&install_lock_name="+ URLEncoder.encode( "南天大门", "utf-8");
  67. String install_address = "&install_address="+ URLEncoder.encode( "北京育新", "utf-8");
  68. String install_gps = "&install_gps="+ URLEncoder.encode( "116.350888,40.011001", "utf-8");
  69. String install_work = "&install_work="+ URLEncoder.encode( "小李", "utf-8");
  70. String install_telete = "&install_telete="+ URLEncoder.encode( "13000000000", "utf-8");
  71. String intall_comm = "&intall_comm="+ URLEncoder.encode( "一切正常", "utf-8");
  72. // 格式 parm = aaa=111&bbb=222&ccc=333&ddd=444
  73. String parm = app_key+ agt_num+ pid+ departid+ install_lock_name+ install_address+ install_gps+ install_work+ install_telete+ intall_comm;
  74. // 将参数输出到连接
  75. dataout.writeBytes(parm);
  76. // 输出完成后刷新并关闭流
  77. dataout.flush();
  78. dataout.close(); // 重要且易忽略步骤 (关闭流,切记!)
  79. // System.out.println(connection.getResponseCode());
  80. // 连接发起请求,处理服务器响应 (从连接获取到输入流并包装为bufferedReader)
  81. BufferedReader bf = new BufferedReader( new InputStreamReader(connection.getInputStream(), "UTF-8"));
  82. String line;
  83. StringBuilder sb = new StringBuilder(); // 用来存储响应数据
  84. // 循环读取流,若不到结尾处
  85. while ((line = bf.readLine()) != null) {
  86. // sb.append(bf.readLine());
  87. sb.append(line).append(System.getProperty( "line.separator"));
  88. }
  89. bf.close(); // 重要且易忽略步骤 (关闭流,切记!)
  90. connection.disconnect(); // 销毁连接
  91. System.out.println(sb.toString());
  92. } catch (Exception e) {
  93. e.printStackTrace();
  94. }
  95. }
  96. public static void main(String[] args) {
  97. // httpURLConectionGET();
  98. httpURLConnectionPOST();
  99. }
  100. }


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


  1. {
  2. "status" : "fail",
  3. "code" : "ERR001",
  4. "msg" : "商户10111不存在"
  5. }

猜你喜欢

转载自blog.csdn.net/zty1317313805/article/details/81028543