SOAP 消息作为字符串发送到 Java 中的端点

SOAP Message as a String Sent to End Point in Java

我有一个字符串,它是一个 SOAP 消息。

String message = "<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header>
        <xyz:Config xmlns:hal="http://example.com/xyz" applicationId="Client" conversationId="000" host="ENDPOINT">
</xyz:Config>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <PaymentRQ xmlns="http://www.iata.org/IATA/4/0"
xmlns:common="http://www.iata.org/IATA/common/4/0">
            <PaymentDetails>
                  <PaymentDetail>
                        <common:PaymentCard CardNumber="1233444444444" CardType="100" ExpireDate="1120" SeriesCode="123">
                  </PaymentDetail>
            </PaymentDetails>
      </PaymentRQ>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
";

我需要将此作为 SOAP 请求发送到 8080 上的本地服务器 运行 所以,url 将是 http://localhost:8080/XYZService/xyz

然后获取 SOAP 响应并读取其值。

请帮助我如何将字符串作为 SOAP 消息发送到上述 url。提前致谢。

如果没有,您可以将 null 传递给 SOAPAction。 对于 serverAddress 传递 serverIp + ServerPort(例如:172...*:8088).

 public  String sendSoapRequest(String serverAdress, String message , String SOAPAction)
        {
            OutputStream httpOutputStream = null;
            byte[] byteArrayStream = message .getBytes();

            URL url = null;
            try {
                url = new URL("http://"+serverAdress);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }

            HttpURLConnection httpURLConnection = null;
            try {
                httpURLConnection = (HttpURLConnection)url.openConnection();
            } catch (IOException e) {
                e.printStackTrace();
            }

            // Set the appropriate HTTP parameters.
            httpURLConnection.setRequestProperty("Content-Length",
                    String.valueOf(byteArrayStream.length));
            httpURLConnection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            httpURLConnection.setRequestProperty("SOAPAction", SOAPAction);
            try {
                httpURLConnection.setRequestMethod("POST");
            } catch (ProtocolException e) {
                e.printStackTrace();
            }
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            try {
                httpOutputStream = httpURLConnection.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }

            //Write the content of the request to the outputstream of the HTTP Connection.
            try {
                httpOutputStream.write(byteArrayStream);
                httpOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            BufferedReader httpInputBuufferedReader = null;
            try {
                httpInputBuufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
            } catch (IOException e) {
                e.printStackTrace();
                Log.e(LOG_TAG,"IOException reading HTTP Input message ");
                return null;
            }

            //Write the SOAP message response to a String.
            StringBuilder returnOutputString = new StringBuilder();
            try {
                String line = "";
                while ((line = httpInputBuufferedReader.readLine()) != null) {
                    returnOutputString.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
                Log.e(LOG_TAG, "IOEception while reading HTTP input buffered reading");
            }
            return returnOutputString.toString();
        }