无法从java程序运行curl命令



在我的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程序执行相同的命令时,它抛出错误。下面是我的代码片段。

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命令。

不要使用curl。你不需要它。你有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());

您最初的尝试失败,因为单参数运行时。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());
}

最新更新