【问题标题】:SOAP Message as a String Sent to End Point in JavaSOAP 消息作为字符串发送到 Java 中的端点
【发布时间】:2018-07-23 13:07:25
【问题描述】:

我有一个字符串,它是一个 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 上运行的本地服务器 所以,网址是http://localhost:8080/XYZService/xyz

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

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

【问题讨论】:

标签: java web-services soap soapui soap-client


【解决方案1】:

如果没有,您可以将 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();
        }

【讨论】:

  • 这很容易。你是冠军!非常感谢基兰。!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多