带附件的ColdFusion SOAP响应

时间:2011-02-14 00:56:46

标签: soap coldfusion

我正在使用Coldfusion9与第三方SOAP服务进行交互,我需要使用它来发送和接收带附件的SOAP。通过在HTTP内容周围使用ToString()将SOAP Body转换为可用的字符串,我在接收可能有也可能没有二进制附件的SOAP方面没有任何问题,但是服务要求我也使用附件发回我的响应这是我要撤消的地方。我在ColdFusion中从未这样做过,我不确定如何将其呈现给原始服务,以便通过ID引用SOAP主体。

下面是使用附件解析传入的SOAP数据:

<cfset soapData = GetHttpRequestData()>

<!--- Loop over the HTTP headers and dump the SOAP content into a variable --->
<cfsavecontent variable="soapContent">
<cfoutput>      
    <cfloop collection = #soapData.headers# item = "http_item">
    #http_item#: #StructFind(soapData.headers, http_item)# #chr(10)##chr(13)# 
    </cfloop>
    request_method: #soapData.method# #chr(10)##chr(13)# 
    server_protocol: #soapData.protocol# #chr(10)##chr(13)# 
    http_content --- #chr(10)##chr(13)#  
    #toString(soapData.content)#
</cfoutput>
</cfsavecontent>

<!--- Save file to flat file --->
<cffile action = "write" 
    file = "#expandPath('../')#logs/#dateFormat(now(),'dd-mm-yyyy')#_#timeFormat(now(),'HHmmss')#.txt" 
    output = "#soapContent#">

现在我正在将响应呈现为一个完整的SOAP XML响应,其中包含带有所需STATUSCODE的内联XML的主体(见下文)。

<cfsavecontent variable="strResponse">
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAPENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
    <SOAPENV:Body>
        <ns1:processResponse xmlns:ns1="urn:TripFlow" SOAPENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
            <STATUSLVL>00</STATUSLVL>
        </ns1:processResponse>
    </SOAP-ENV:Body>
</SOAPENV:Envelope>
</cfsavecontent>

<!--- Strip all whitespace between tags --->
<cfset strResponse = trim(ReReplaceNoCase(strResponse,'(>[\s]*<)','><','ALL'))>

<!--- Output the XML response to the soap service --->
<cfoutput>#strResponse#</cfoutput>

上述响应引发了一个错误,因为SOAP服务要求发送响应作为附件引用正文消息,如下文所示:

HTTP/1.1 200 OK
Date: Thu, 01 Apr 2010 09:30:25 GMT
Server: Jetty/5.1.4 (Windows XP/5.1 x86 java/1.5.0_15
Content-Type: multipart/related; boundary=soaptestserver; type="text/xml"; start="<theenvelope>"
SOAPAction: ""
Content-Length: 796
Connection: close

--soaptestserver
Content-ID: <theenvelope>
Content-Transfer-Encoding: 8bit
Content-Type: text/xml; charset=utf-8
Content-Length: 442

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAPENV="
http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/1999/XMLSchema"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"><SOAPENV:
Body><ns1:processResponse xmlns:ns1="urn:TripFlow" SOAPENV:
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><message
href="cid:thecontentmessage"/></ns1:processResponse></SOAP-ENV:Body></SOAPENV:
Envelope>

--soaptestserver
SOAP Interface
www.travelsolutions.com 123
travel solutions online V14.0 External System Integration
Content-ID: <thecontentmessage>
Content-Transfer-Encoding: 8bit
Content-Type: text/xml; charset=utf-8
Content-Length: 65

<?xml version="1.0" encoding="UTF-8"?><STATUSLVL>00</STATUSLVL>
--soaptestserver--

任何帮助都会非常感激,因为我真的在这个墙上碰到了我的头。谢谢!

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

每当我与SOAP服务交互时,我通常最终会使用与此类似的东西。它通常有效。请注意,我在那里有一些占位符文本,您需要替换为第三方提供商的相应值。

<cfsavecontent variable="soap">
<?xml version="1.0" encoding="UTF-8" ?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
   <soapenv:Header/>
   <soapenv:Body>
      <ns1:processResponse xmlns:ns1="urn:TripFlow" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <statuslvl>00</statuslvl>
      </ns1:processResponse>
   </soapenv:Body>
</soapenv:Envelope>
</cfsavecontent>

<!--- Invoke web service to send message--->
<cfhttp url="http://3rd-party-url-here" method="post" timeout="10">
<cfhttpparam type="header" name="content-type" value="text/xml" />
<cfhttpparam type="header" name="SOAPAction" value="""3rd-party-method-name-here""" />
<!---<cfhttpparam type="header" name="accept-encoding" value="no-compression" />  sometimes this is needed --->
<cfhttpparam type="header" name="content-length" value="#len(soap)#" />
<cfhttpparam type="header" name="charset" value="utf-8" />
<cfhttpparam type="xml" name="message" value="#trim(soap)#" />
</cfhttp>