欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java调用接口的get方法

程序员文章站 2022-12-30 23:07:48
java 调用接口的get方法//接口调用 public String get(String httpUrl) { // httpurl为所调用接口的url String result = ""; try { URL url = new URL(httpUrl.toString()); StringBuffer document = new StringBuffer();......
java 调用接口的get方法

 //接口调用
    public String get(String httpUrl) { // httpurl为所调用接口的url
        String result = "";
        try {
            URL url = new URL(httpUrl.toString());
            StringBuffer document = new StringBuffer();
            HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
            httpconn.setRequestProperty("accept", "*/*");
            httpconn.setRequestProperty("connection", "Keep-Alive");
            httpconn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
//建立实际的连接
            httpconn.connect();
            InputStream input = null;
            try {
                httpconn.connect();
                input = httpconn.getInputStream();
            } catch (Exception e) {
                e.printStackTrace();
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            String Result = "";
            while ((Result = reader.readLine()) != null) {
                document.append(Result);
            }
            result = document.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
 

 

本文地址:https://blog.csdn.net/jiandan7410/article/details/107493369