zoukankan      html  css  js  c++  java
  • JAVA如何跨项目调用接口

    public String load(String url, String query) throws Exception {
            URL restURL = new URL(url);
            /*
             * 此处的urlConnection对象实际上是根据URL的请求协议(此处是http)生成的URLConnection类 的子类HttpURLConnection
             */
            HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();
            //请求方式
            conn.setRequestMethod("POST");
            //设置是否从httpUrlConnection读入,默认情况下是true; httpUrlConnection.setDoInput(true);
            conn.setDoOutput(true);
            //allowUserInteraction 如果为 true,则在允许用户交互(例如弹出一个验证对话框)的上下文中对此 URL 进行检查。
            conn.setAllowUserInteraction(false);
    
            PrintStream ps = new PrintStream(conn.getOutputStream());
            ps.print(query);
            ps.close();
    
            BufferedReader bReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    
            String line, resultStr = "";
    
            while (null != (line = bReader.readLine())) {
                resultStr += line;
            }
            System.out.println("3412412---" + resultStr);
            bReader.close();
            return resultStr;
        }
      public static void main(String[] args) {
            try {
                APIService restUtil = new APIService();
                String resultString = restUtil.load(
                        "http://**.**.**.**:8079/HouseLizardCloud/Message/ShortMessage",
                        "mobile=177198****&codeType=2");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.print(e.getMessage());
            }
        }
    }
    

      

  • 相关阅读:
    javascript 构造函数,工厂模式,稳妥构造函数浅析
    javascript基本概念
    struts2 类型转换
    struts action
    struts2 配置(部分)
    struts2基本构成
    charapter 1
    java 内部类
    mysql zip 解压安装 (win10)
    python之random模块
  • 原文地址:https://www.cnblogs.com/EveningWind/p/10070397.html
Copyright © 2011-2022 走看看