悠然哈哈哈

导航

Java 模拟curl请求

D:\MyData>curl http://10.10.109.127:8088/s -d link=http://www.baidu.com
返回如下:
8lngKnjTC5k

  

 static String[] cmdParts3 = {"curl","http://10.16.149.127:8088/s", "-d", "link=http://www.baidu.com"};

 public static void main(String[] args) {

        System.out.println(execCmdParts(cmdParts3));
    }

    public static String execCmdParts(String[] cmdParts) {
        ProcessBuilder process = new ProcessBuilder(cmdParts);
        Process p;
        try {
            p = process.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }

            return builder.toString();
        } catch (IOException e) {
            System.out.print("error");
            e.printStackTrace();
        }

        return null;
    }

参考:https://blog.51cto.com/u_16099188/6388566

posted on 2023-09-11 19:50  悠然886  阅读(16)  评论(0编辑  收藏  举报