Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从java运行curl命令?_Java_Spring_Curl - Fatal编程技术网

如何从java运行curl命令?

如何从java运行curl命令?,java,spring,curl,Java,Spring,Curl,我正在尝试使用curl从Java发出POST请求。它也有一些有效载荷。我的状态代码是400。我错过了什么 下面是我在terminal中使用的旋度 curl --cookie "token=xyzxyzxyzxyz" --header "Content-Type:application/json" --data {"branchName":"name","branchId":"bid","sourceBranch":"sb","alias":"pppp","mainPackageFullPath

我正在尝试使用curl从Java发出POST请求。它也有一些有效载荷。我的状态代码是400。我错过了什么

下面是我在terminal中使用的旋度

curl --cookie "token=xyzxyzxyzxyz" --header "Content-Type:application/json" --data 
{"branchName":"name","branchId":"bid","sourceBranch":"sb","alias":"pppp","mainPackageFullPath":"main.full.path"}'
-k http://app.aws.application/api/random/create

你可以这样做

    String branchName="name";
    String branchId="bid";
    String sourceBranch="sb"
    String alias="ppp"

   String[] command = {"curl" "-k" "-i" "-X" POST "-H" "Content-Type: multipart/form-data" --cookie "rsession=your rsession" "Content-Type:application/json" --data{branchName+":"+branchId":"+sourceBranch":",+alias}};
    ProcessBuilder process = new ProcessBuilder(command); 
    Process p;
    try
    {
        p = process.start();
         BufferedReader reader =  new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ( (line = reader.readLine()) != null) {
                    builder.append(line);
                    builder.append(System.getProperty("line.separator"));
            }
            String result = builder.toString();
            System.out.print(result);

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

你可以这样做

    String branchName="name";
    String branchId="bid";
    String sourceBranch="sb"
    String alias="ppp"

   String[] command = {"curl" "-k" "-i" "-X" POST "-H" "Content-Type: multipart/form-data" --cookie "rsession=your rsession" "Content-Type:application/json" --data{branchName+":"+branchId":"+sourceBranch":",+alias}};
    ProcessBuilder process = new ProcessBuilder(command); 
    Process p;
    try
    {
        p = process.start();
         BufferedReader reader =  new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ( (line = reader.readLine()) != null) {
                    builder.append(line);
                    builder.append(System.getProperty("line.separator"));
            }
            String result = builder.toString();
            System.out.print(result);

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

你应该加上你试过的。另外,我不确定您的用例是什么,但是您最好直接通过java发出post请求,而不是运行curl命令。另外,我不确定您的用例是什么,但是您最好直接通过java发出post请求,而不是运行curl命令。