gpt4 book ai didi

java - 使用 Java 将 SOAP 消息格式转换为 Socket 消息格式,反之亦然

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:27:21 25 4
gpt4 key购买 nike

我目前正在研究使用 Java 将 SOAP 消息格式转换为 Socket 消息格式,反之亦然。

我需要它来重用读取套接字格式消息的旧系统以连接到发送和接收 SOAP 消息格式的网站。

我应该怎么做?我应该考虑文本处理吗?

SOAP 示例套接字

socket

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Interface Code="20"
<Transaction Txn="01880120121024000001" CD="01880120121024000001001"
Date="2012-10-24 17:27:25" BirthDate="1983-03-27" Code="8110009000000720" Type="0"/>
</Interface>

SOAP

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<webRequest xmlns="http://____________">
<arg0 xmlns="">&lt;?xml version="1.0" encoding="UTF-8"
standalone="yes"?>&lt;Interface xmlns="http://____________"
Version="1.0" Code="20" Txn="123" CD="456">&lt;Info
BirthDate="1983-03-27" Code="1234" Type="0" />&lt;/Interface></arg0>
</webRequest>
</soapenv:Body>
</soapenv:Envelope>

最佳答案

套接字消息是 SOAP 消息的 XML 转义主体。您不需要额外的库,因为 parsing SOAP requests 有 javax 类。

SOAP 到套接字很简单:

String message = "<?xml version='1.0' encoding='UTF-8'?>\n<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\n <soapenv:Body>\n  <webRequest xmlns=\"http://____________\">\n   <arg0 xmlns=\"\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"\n    standalone=\"yes\"?>&lt;Interface xmlns=\"http://____________\"\n    Version=\"1.0\" Code=\"20\" Txn=\"123\" CD=\"456\">&lt;Info\n    BirthDate=\"1983-03-27\" Code=\"1234\" Type=\"0\" />&lt;/Interface></arg0>\n  </webRequest>\n </soapenv:Body>\n</soapenv:Envelope>";
InputStream is = new ByteArrayInputStream(message.getBytes());
SOAPMessage request = MessageFactory.newInstance().createMessage(null, is);
System.out.println(request.getSOAPBody().getTextContent());

SOAP 的套接字更复杂,因为我们需要创建 webRequest 包装元素:

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
docFactory.setNamespaceAware(true); // webRequest needs a namespace
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

Document doc = docBuilder.newDocument();
Element root = doc.createElementNS("http://____________", "webRequest");
doc.appendChild(root);

Element argElement = doc.createElement("arg0");
root.appendChild(argElement);
String message = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<Interface Code=\"20\" \n <Transaction Txn=\"01880120121024000001\" CD=\"01880120121024000001001\" \n Date=\"2012-10-24 17:27:25\" BirthDate=\"1983-03-27\" Code=\"8110009000000720\" Type=\"0\"/>\n</Interface>";
argElement.setTextContent(message);

SOAPMessage request = MessageFactory.newInstance().createMessage();
request.getSOAPBody().addDocument(doc);
request.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
request.writeTo(System.out);

关于java - 使用 Java 将 SOAP 消息格式转换为 Socket 消息格式,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32556509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com