如何获取url返回的数据

Java 获取url返回数据的两种情况:

1.无需验证的

//url example: http://xxxxx-data
public static String sendGet(String url){

        StringBuffer buffer = new StringBuffer();
        HttpURLConnection httpConn = null;
        BufferedReader reader = null;
        try {

            URL realUrl = new URL(url);
            httpConn = (HttpURLConnection) realUrl.openConnection();
            httpConn.setRequestMethod("GET");

            //设置token
            httpConn.connect();

            reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {

                buffer.append(line);
            }
        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            if (httpConn != null) {
                httpConn.disconnect();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return buffer.toString();
        }
    }

 

2.需要用户名密码登录验证的

这里可以分成两步走, 先发post请求,获取token, 然后发get请求,获取数据。

2.1发post请求获取token:

//url example: http://xxxxx-login?user=aaaa&pwd=bbbbb
public static String sendPost(String url) {

        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {

            URL realUrl = new URL(url);

            //新建URL链接
            URLConnection conn = realUrl.openConnection();

            //设置请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

            //设置发送POST请求的参数
            conn.setDoOutput(true);
            conn.setDoInput(true);

            //获取URL的输出流
            out = new PrintWriter(conn.getOutputStream());

            // flush输出流的缓冲
            out.flush();

            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            String line;

            while ((line = in.readLine()) != null) {

                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求失败!"+e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }

            return result;
        }
    }

2.2.将post请求中的token,设置到get请求中获取数据:

 //url example: http://xxxxx-data
 public static String sendGet(String url, String token){

        StringBuffer buffer = new StringBuffer();
        HttpURLConnection httpConn = null;
        BufferedReader reader = null;
        try {
            
            URL realUrl = new URL(url);
            httpConn = (HttpURLConnection) realUrl.openConnection();
            httpConn.setRequestMethod("GET");

            //设置token
            httpConn.setRequestProperty("Authorization", token);
            httpConn.connect();

            reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {

                buffer.append(line);
            }
        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            if (httpConn != null) {
                httpConn.disconnect();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return buffer.toString();
        }
    }

参考资料: https://blog.csdn.net/hui_hong_tailang/article/details/82901030

你可能感兴趣的:(Java后端)