如何在Java中将字符串转换为SOAPMessage? [英] How to convert a string to a SOAPMessage in Java?

查看:131
本文介绍了如何在Java中将字符串转换为SOAPMessage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有没有办法将字符串转换为 SOAPMessage

I wonder is there any way to convert a string to SOAPMessage?

让我说我有一个字符串如下:

Let me say I have a string as follows:

String send = "<soap:Envelope xmlns:mrns0=\"http://sdp.SOMETHING.com/mapping/TSO\" xmlns:sdp=\"http://sdp.SOMETHING.com.tr/mapping/generated\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
        + "<soap:Header>"
        + "<sdp:token>"
        + "<sdp:sessionId>" + sessionId + "</sdp:sessionId>"
        + "</sdp:token>"
        + "<sdp:transaction-list>"
        + "<sdp:transaction-id>" + 11 + "</sdp:transaction-id>"
        + "</sdp:transaction-list>"
        + "</soap:Header>"
        + "<soap:Body>"
        + "<sdp:SendSMSInput>"
        + "<sdp:EXPIRY_DATE>" + extime + "</sdp:EXPIRY_DATE>"
        + "<sdp:MESSAGE_CLASS>0</sdp:MESSAGE_CLASS>"
        + "<sdp:S_DATE>" + time + "</sdp:S_DATE>"
        + "<sdp:SHORT_NUMBER>1905</sdp:SHORT_NUMBER>"
        + "<sdp:SRC_MSISDN>" + numSend + "</sdp:SRC_MSISDN>"
        + "<sdp:TO_RECEIVERS>"
        + "<sdp:msisdn>" + numSend + "</sdp:msisdn>"
        + "</sdp:TO_RECEIVERS>"
        + "<sdp:MESSAGE_BODY>"
        + "<sdp:message>Message body here.</sdp:message>"
        + "</sdp:MESSAGE_BODY>"
        + "</sdp:SendSMSInput>"
        + "</soap:Body>"
        + "</soap:Envelope>";



当我这样做时:


When I do this:

    SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
    SOAPConnection connection = sfc.createConnection();
    InputStream is = new ByteArrayInputStream(send.getBytes());
    SOAPMessage request = MessageFactory.newInstance().createMessage(null, is);
    request.removeAllAttachments();

    SOAPPart part = request.getSOAPPart();
    part.detachNode();
    SOAPEnvelope env = part.getEnvelope();
    env.detachNode();
    SOAPBody body = env.getBody();
    body.detachNode();
    SOAPHeader head = env.getHeader();
    head.detachNode();

    request.writeTo(System.out);

    URL endpoint = new URL("http://sdp.somewhere.com.tr/view/LbsOpaqueService.wsdl");
    SOAPMessage response = connection.call(request, endpoint);
    System.out.println(response.getContentDescription());

一切正常。但我仍然从服务器获得 NULL 。可能是什么原因?我改变了<和>转义字符。

Everything works fine. But I still get NULL from server. What could be the reason? I have changed < and > to escape chars.

推荐答案

将字符串转换为输入流,然后将其读入SOAP消息工厂。

Convert the String into an input stream, then read it into the SOAP message factory.

InputStream is = new ByteArrayInputStream(send.getBytes());
SOAPMessage request = MessageFactory.newInstance().createMessage(null, is);

您可以在此处阅读有关如何执行此操作的信息。

这篇关于如何在Java中将字符串转换为SOAPMessage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆