简体   繁体   English

Java 运行 curl 命令在 Windows 和 ZEDC9F0A5A5D577978368E3736Z47 之间是不同的

[英]Java run curl command is different between Windows and Linux

I use Spring Boot to write a api server, run on 192.168.24.102:7000,which accept restful api to create an app.我使用 Spring Boot 来编写 api 服务器,在 192.168.24.102:7000 上运行,它接受宁静的 api 来创建应用程序。 Here is the problem I meet and even I finally solve it but I don't understand why?这是我遇到的问题,即使我终于解决了,但我不明白为什么?

When the curl command run on Linux, it is success, like this:在 Linux 上运行 curl 命令就成功了,像这样:

curl " https://192.168.24.102:7000/app/registerbyip " -X POST -H "Content-Type: application/json" -k -d "{\"ip\": \"192.168.24.102\", \"appType\": 5, \"appName\": \"efg\"}" curl " https://192.168.24.102:7000/app/registerbyip " -X POST -H "Content-Type: application/json" -k -d "{\"ip\.410"2\".192,102:7000/app/registerbyip" \"appType\": 5, \"appName\": \"efg\"}"

在此处输入图像描述

When the curl command run on Windows cmd it is also success, like this:当 curl 命令在 Windows cmd 上运行时,它也是成功的,如下所示:

在此处输入图像描述

So the curl command is right, then I write it into Java code所以curl命令是对的,那我写成Java代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestDemo {
    public static String exeCurl(String[] cmds) {
        ProcessBuilder process = new ProcessBuilder(cmds);
        Process p;
        try {
            p = process.start();

            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append(System.getProperty("line.separator"));
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {        
        String[] cmd = {"curl",
            "https://192.168.24.102:7000/app/registerbyip", "-k", "-H",
            "Content-Type: application/json;charset=UTF-8",
            "-X", "POST",
            "-d", "{\"ip\": \"192.168.24.102\", \"appType\": 5, \"appName\": \"efg\"}"};
        System.out.println(exeCurl(cmd));
    }
}

when this java code run on windows, the server will report exception like below:当此 java 代码在 windows 上运行时,服务器将报告如下异常:

{"timestamp":"2019-11-15T02:09:33.261+0000","status":400,"error":"Bad Request","message":"JSON parse error: Unexpected character ('i' (code 105)): was expecting double-quote to start field name; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('i' (code 105)): was expecting double-quote to start field name\n at [Source: (PushbackInputStream); line: 1, column: 3]","path":"/app/registerbyip"} {"timestamp":"2019-11-15T02:09:33.261+0000","status":400,"error":"Bad Request","message":"JSON 解析错误:意外字符 ('i' (代码 105)):期待双引号开始字段名称;嵌套异常是 com.fasterxml.jackson.core.JsonParseException:意外字符('i'(代码 105)\):期待双引号开始字段名称n 在 [Source: (PushbackInputStream); line: 1, column: 3]","path":"/app/registerbyip"}

But I copy the java.class file to the linux, the run it will be success.但是我把java.class文件复制到linux,运行就成功了。 Why?为什么? the same code run on Windows will fail but in linux will success.在 Windows 上运行的相同代码将失败,但在 linux 中将成功。

Finally I fix the java curl code like this:最后,我修复了 java curl 代码,如下所示:

String[] cmd = {"curl",
    "https://192.168.24.102:7000/app/registerbyip", "-k", "-H",
    "Content-Type: application/json;charset=UTF-8",
    "-X", "POST",
    "-d", "{\"\"ip\"\": \"\"192.168.24.102\"\", \"\"appType\"\": 5, \"\"appName\"\": \"\"efg\"\"}"};

Then it will be success by add another \", but this java code will fail when run on Linux.然后再添加一个\"就会成功,但是这个java代码在Linux上运行时会失败。

在此处输入图像描述

So here is my question?所以这是我的问题? Why will it happen?为什么会发生? Why the java code should add another \" sign? Please help, Thanks a lot.为什么 java 代码应该添加另一个 \" 符号?请帮助,非常感谢。

Any specific reason why you're using curl for this?您为此使用 curl 的任何具体原因? You can do exactly what you are doing there in pure Java with no curl and it will be easier and work great on Windows, Linux and whatever else. You can do exactly what you are doing there in pure Java with no curl and it will be easier and work great on Windows, Linux and whatever else. In Java 11 and later, use the new HttpClient .在 Java 11 及更高版本中,使用新的 HttpClient It's very simple.这很简单。 In earlier versions of Java, you can use the HttpURLConnection , but it's probably better to upgrade to 11 at this point.在 Java 的早期版本中,您可以使用HttpURLConnection ,但此时升级到 11 可能会更好。

It is because the client curl perform differently on different platform!这是因为客户端 curl 在不同平台上的表现不同!

On windows, the param curl -d content,the string content show like:在 windows 上,参数 curl -d 内容,字符串内容显示如下:

{\"a\":\"123\"}  (in java "{\\\"a\\\" : \\\"123\\\"}")

or或者

{""a"":""123""}  (in java "{\"\"a\"\" : \"\"123\"\"}")

can be considered as {"a":"123"} by client curl.客户端 curl 可以将其视为{"a":"123"}

On linux or mac, the -d content shown like: {"a":"123"} , the curl consider it as {"a":"123"} .在 linux 或 mac 上, -d 内容显示为: {"a":"123"} , curl 将其视为{"a":"123"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM