【问题标题】:Unable to run curl command from a java program无法从 java 程序运行 curl 命令
【发布时间】:2021-10-26 11:12:14
【问题描述】:

当我从我的 linux 机器上执行以下 curl 命令时,我能够毫无问题地获得响应。

curl -X POST -H "Content-Type: text/xml"  -H 'SOAPAction: "someAction"' -d '<soapenv:Envelope xmlns:soapenv="http://somenamespace/">   <soapenv:Header/>   <soapenv:Body> DATA</soapenv:Body></soapenv:Envelope>' https://somewebservice/

     

但是当我尝试从 java 程序执行相同的命令时,它会抛出错误。 下面是我的代码 sn-p。

    try {
                    String xmlInut = "'<soapenv:Envelope xmlns:soapenv=\"http://somenamespace/\">   <soapenv:Header/>   <soapenv:Body> DATA</soapenv:Body></soapenv:Envelope>'"
                    String curlCommand = "curl -X POST -H \"Content-Type: text/xml\"  -H 'SOAPAction: \"GenerateToken\"' -d "+xmlInput+" https://somewebservice/";
                    System.out.println(curlCommand);
                    Process process = Runtime.getRuntime().exec(curlCommand);
                    BufferedReader input = new BufferedReader(new     InputStreamReader(process.getInputStream()));
                    outputString = "";
                    String line=null;
                    while((line=input.readLine())!=null)
                    {
                        outputString+=line;
                    }
                    System.out.println(outputString);

这是我遇到的错误

    <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<soapenv:Fault>
<faultcode/>
<faultstring>com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character ''' (code 39) in prolog; expected '&lt;' at [row,col {unknown-source}]: [1,1]
</faultstring></soapenv:Fault></soapenv:Body></soapenv:Envelope>

如何解决此错误?我在命名空间和数据周围尝试了单引号和双引号的各种组合,但它们都不起作用。 我需要从我的 java 代码中执行相同的 curl 命令。

【问题讨论】:

  • 您是否需要通过 Java 运行 curl 才能调用远程 SOAP 端点?你可以通过 Java 代码做到这一点。

标签: java curl soap


【解决方案1】:

不要使用卷曲。你不需要它。你有 Java:

String xmlInput =
    "<soapenv:Envelope xmlns:soapenv=\"http://somenamespace/\">" +
    "    <soapenv:Header/>" +
    "    <soapenv:Body> DATA</soapenv:Body>" +
    "</soapenv:Envelope>";

URL url = new URL(https://somewebservice/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/xml");
conn.setRequestProperty("SOAPAction", "GenerateToken");

conn.setDoOutput(true);
try (Writer requestBody =
    new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8)) {

    requestBody.write(xmlInput);
}

int responseCode = conn.getResponseCode();
if (responseCode >= 400) {
    throw new IOException("Service at " + url + " returned " + responseCode);
}

从 Java 11 开始,您可以改用高级的 java.net.http 包:

String xmlInput =
    "<soapenv:Envelope xmlns:soapenv=\"http://somenamespace/\">" +
    "    <soapenv:Header/>" +
    "    <soapenv:Body> DATA</soapenv:Body>" +
    "</soapenv:Envelope>";

URI uri = new URI("https://somewebservice/");

HttpRequest request = HttpRequest.newBuilder(uri)
    .setHeader("Content-Type", "text/xml");
    .setHeader("SOAPAction", "GenerateToken");
    .POST(HttpRequest.BodyPublishers.ofString(xmlInput))
    .build();

String outputString =
    HttpClient.newHttpClient().send(request,
        HttpResponse.BodyHandlers.ofString());

您最初的尝试失败了,因为单参数 Runtime.exec 试图解析您的命令,就像在 shell 中一样。我希望现在很清楚您不应该依赖 curl,但如果必须,请使用 ProcessBuilder,而不是 Runtime.exec:

ProcessBuilder builder = new ProcessBuilder("curl", "-X", "POST",
    "-H", "Content-Type: text/xml", "-H", "SOAPAction: GenerateToken",
    "-d", xmlInput, "https://somewebservice/");

builder.redirectError(ProcessBuilder.Redirect.INHERIT);

Process process = builder.start();

String outputString;
try (InputStream processOutput = process.getInputStream()) {
    outputString = new String(processOutput.readAllBytes());
}

int exitCode = process.waitFor();
if (exitCode != 0) {
    throw new IOException("Exit code " + exitCode + " was returned by "
        + builder.command());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    相关资源
    最近更新 更多