CURL请求工具

自己写了一点请求工具。只实验了两个是对的,其他的没用到,要用的就拿去。借鉴他人的,整改了一下,各位老哥用的话随便拿去,有错的话请给小弟指出来,多谢多谢

import io.ctc.integration.common.CURLConstant;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * CURL 命令请求工具
 * @Date 2021/11/22 18:42
 * @Version 1.0
 */

public class CURLUtils {


    /**
     * 1.curl 最简单的命令是 curl URL,以下输入将返回请求地址的内容
     * 例如:
     * curl http://localhost:8080/home/getTest
     * @param url
     * @return
     */
    public static String post(String url){
        String[] cmds = {CURLConstant.CURL,url};
        return execCurl(cmds);
    }

    /**
     * 2.通过-i 参数返回,还返回 HTTP 头,例如:
     * curl -i -G http://localhost:1001/GetTest/getTest
     * @param url
     * @return
     */
    public static String reqData(String url){
        String[] cmds = {
                CURLConstant.CURL,
                url,
                CURLConstant.I,
                CURLConstant.G};
        return execCurl(cmds);
    }


    /**
     * 3.URL 通常用双引号防止转义,比如&符号在命令行中表示后台运行,如果参数是跟在
     * 地址后面的,比如双引号
     * 例如:
     * curl “http://localhost:8080/GetTest/getTest?q=txt&c=1”
     * @param url
     * @param map
     * @return
     */
    public static String reqData(String url, Map<String,String> map){
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            sb.append(entry.getKey()+"="+entry.getValue()+"&");
        }
        String substring = sb.substring(0,sb.length() - 1);
        String str = url+"?"+substring;
        String[] cmds = {
                CURLConstant.CURL,
                str};
        return execCurl(cmds);
    }



    /**
     * 4.通过-H 设直请求的 HTTP 头,比如请求体是 JSON 格式,后面是没有参数的
     * 例如:
     * curl -H "Content-Type:application/json" http://localhost:8080/GetTest/getTest
     * @param url 地址
     * @param headers 请求头
     * @param request 请求方法是post还是get
     * @return
     */
    public static String post(String url, List<String> headers,CURLConstant request){

        List<String> list = new ArrayList<String>(){
            {
                add(CURLConstant.CURL);
                add(CURLConstant.X);
                add(String.valueOf(request));
            }
        };
        headers.stream().forEach(a ->{
            list.add(CURLConstant.H);
            list.add(a);
        });
        list.add(url);

        //list转为数组
        String[] arr = new String[list.size()];

        return execCurl(list.toArray(arr));
    }


    /**
     * 5.通过-d 参数发起 POST 请求,-d 后面是 POST 的内容,
     * 例如:
     * curl http://localhost:1001/GetTest/getTest -d "name=tom&password=123"
     * @param url
     * @param map
     * @return
     */
    public static String post(String url,Map<String,String> map){
        StringBuffer sb = new StringBuffer();
        List<String> list = new ArrayList<String>(){
            {
                add(CURLConstant.CURL);
                add(url);
                add(CURLConstant.D);
            }
        };

        for (Map.Entry<String, String> entry : map.entrySet()) {
            sb.append(entry.getKey()+"="+entry.getValue()+"&");
        }

        String substring = sb.substring(0,sb.length() - 1);
        list.add(substring);

        String[] arr = new String[list.size()];

        return execCurl(list.toArray(arr));
    }


    /**
     * 这个应该是很多人要用的,我们就是用的这个来请求的
     * 例如:
     * curl
     * -X POST "http://localhost:1001/GetTest/getTest"
     * -H "Content-Type:application/json"
     * -d ""dataparams":{ "name":"北京 100 种小吃","type":"food","postDate":"2009-11- 15"}
     * @param url 地址
     * @param headers 请求头
     * @param requestData 请求要发过去的数据
     * @return
     */
    public static String post(String url, List<String> headers, String requestData){

        List<String> headersList = new ArrayList<String>(){
            {
                add(CURLConstant.CURL);
                add(CURLConstant.X);
                add(CURLConstant.POST);
                add(url);
            }
        };
        headers.stream().forEach(a ->{
            headersList.add(CURLConstant.H);
            headersList.add(a);
        });
        headersList.add(CURLConstant.D);
        headersList.add(requestData);

        String[] arr = new String[headersList.size()];

        return execCurl(headersList.toArray(arr));
    }



    /**
     * 公共调用方法
     * @param cmds
     * @return
     */
    public static String execCurl(String[] cmds) {
        ProcessBuilder process = new ProcessBuilder(cmds);
        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;
    }
}

public class CURLConstant {

    /**
     * curl头
     */
    public static final String CURL = "curl";

    /**
     * 指定请求方法的命令,例如 -X:post,指定post方法
     */
    public static final String X = "-X";

    /**
     * post方法
     */
    public static final String POST = "POST";

    public static final String I = "-i";

    public static final String G = "-G";
    /**
     * 请求头
     */
    public static final String H = "-H";

    public static final String HOST = "Host: cmccdevops-test-pod6-devops.hu6-173-77.4a.cmit.cloud";

    /**
     * 指定数据
     */
    public static final String D = "-d";

    public static final String CONTENT_TYPE = "Content-Type: application/json;charset=UTF-8";

    /**
     * cookie
     */
    public static final String COOKIE = "--cookie";
}

上一篇:Postman 导入curl 、导出成curl、导出成对应语言代码


下一篇:Java异常:RuntimeException和Exception的区别