zoukankan      html  css  js  c++  java
  • Jeecg代码搬砖圣地第六篇(接口交互)

    1.服务端

      1.1 在系统管理---->接口权限------>接口用户管理里录入客户端发送过来的用户名test和密码123456

      1.2 启动redis

    2.客户端代码

    public static String getToken(String userName,String password){
            String url = "http://localhost:8080/jeecg/rest/tokens?username="+userName+"&password="+password;
            String token=httpRequest(url, "POST", null);
            return token;
        }
        
        
        //获取黑名单列表
        public static JSONObject getBlackList(String token){
            String url = "http://localhost:8080/jeecg/rest/tsBlackListController";
            JSONObject resp=httpRequest(url, "GET", null,token);
            return resp;
        }
        
        //创建黑名单
        public static JSONObject createBlackList(String token,String json){
            String url = "http://localhost:8080/jeecg/rest/tsBlackListController";
            JSONObject resp=httpRequest(url, "POST", json,token);
            return resp;
        }
        
        
        //更新黑名单
        public static JSONObject updateBlackList(String token,String json){
            String url = "http://localhost:8080/jeecg/rest/tsBlackListController";
            JSONObject resp=httpRequest(url, "PUT", json,token);
            return resp;
        }
        
        
        //删除黑名单
        public static JSONObject deleteBlackList(String token,String id){
            String url = "http://localhost:8080/jeecg/rest/tsBlackListController/"+id;
            JSONObject resp=httpRequest(url, "DELETE", null,token);
            return resp;
        }
        
        //查询黑名单
        public static JSONObject getBlackList(String token,String id){
            String url = "http://localhost:8080/jeecg/rest/tsBlackListController/"+id;
            JSONObject resp=httpRequest(url, "GET", null,token);
            return resp;
        }
        
        
        public static void main(String[] args) {
            String token = getToken("test","123456");
            System.out.println(" token : "+ token);
    //        String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJhZG1pbiIsInN1YiI6ImFkbWluIiwiaWF0IjoxNTExODU5NjM2fQ.Emfe8VZKA_L33jaW8ZUtVFVDEin83Np_d3gKlPIZryE";
            
            //添加黑名单
    //        JSONObject jsonObject=new JSONObject();
    //        jsonObject.put("ip","192.168.1.2");
    //        System.out.println("======添加黑名单======="+createBlackList(token,jsonObject.toJSONString()));
            //更新黑名单
    //        JSONObject jsonObject=new JSONObject();
    //        jsonObject.put("id","402881ee6001da57016001dc13110001");
    //        jsonObject.put("ip","192.168.0.111");
    //        System.out.println("======更新黑名单======="+updateBlackList(token,jsonObject.toJSONString()));
            //删除黑名单
    //        System.out.println("======删除黑名单======="+deleteBlackList(token,"402881ee6001da57016001dc13110001"));
            //查询黑名单
    //        System.out.println("======查询黑名单======="+getBlackList(token,"402881ee6001e873016001f369f40008"));
            //获取黑名单列表
            System.out.println("======获取黑名单列表======="+getBlackList(token));
        }
    public static String httpRequest(String requestUrl, String requestMethod, String outputStr) {
            String res = "";
            StringBuffer buffer = new StringBuffer();
            HttpURLConnection httpUrlConn = null;
            try {
                // 创建SSLContext对象,并使用我们指定的信任管理器初始化
                URL url = new URL(requestUrl);
                httpUrlConn = (HttpURLConnection) url.openConnection();
                httpUrlConn.setDoOutput(true);
                httpUrlConn.setDoInput(true);
                httpUrlConn.setUseCaches(false);
                httpUrlConn.setRequestProperty("Accept", "text/plain");
                 httpUrlConn.setRequestProperty("Content-Type", "application/json");
                // 设置请求方式(GET/POST)
                httpUrlConn.setRequestMethod(requestMethod);
                if ("GET".equalsIgnoreCase(requestMethod))
                    httpUrlConn.connect();
    
                // 当有数据需要提交时
                if (null != outputStr) {
                    OutputStream outputStream = httpUrlConn.getOutputStream();
                    // 注意编码格式,防止中文乱码
                    outputStream.write(outputStr.getBytes("UTF-8"));
                    outputStream.close();
                }
    
                // 将返回的输入流转换成字符串
                InputStream inputStream = httpUrlConn.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    
                String str = null;
                while ((str = bufferedReader.readLine()) != null) {
                    buffer.append(str);
                }
                bufferedReader.close();
                inputStreamReader.close();
                // 释放资源
                inputStream.close();
                inputStream = null;
                httpUrlConn.disconnect();
                res = buffer.toString();
                log.debug(res);
            } catch (ConnectException ce) {
                LogUtil.info("Weixin server connection timed out.");
            } catch (Exception e) {
                e.printStackTrace();
                org.jeecgframework.core.util.LogUtil.info("https request error:{}" + e.getMessage());
            } finally {
                try {
                    httpUrlConn.disconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                    org.jeecgframework.core.util.LogUtil.info("http close error:{}" + e.getMessage());
                }
            }
            return res;
        }

      

  • 相关阅读:
    你的MongoDB Redis设置用户名密码了吗?看看shodan这款邪恶的搜索引擎吧!~
    聊聊数据库(MySql)连接吧,你真的清楚吗?
    .net Mongo Driver 1.0与2.0的对比与2.0的优化
    浅谈 JavaScript new 执行过程及function原理
    Jquery源码分析与简单模拟实现
    使用HttpWebRequest模拟登陆阿里巴巴(alibaba、httpwebrequest、login)
    Url以.(点)结尾,在使用httpwebrequest读取的时候,微软会有一个bug……
    VS超强调试技巧--忍不住想赞一下
    ClickOnce部署疑难杂症:更新时部署与应用程序标识不一致问题。要安装此应用程序,请修改此文件的清单版本或卸载之前存在的应用程序。
    MAC地址的介绍(单播、广播、组播、数据收发)
  • 原文地址:https://www.cnblogs.com/xujiating/p/12097144.html
Copyright © 2011-2022 走看看