UPDATE: Simon Fell caught me in the midst of evil, as he rightfully points out that when one bypasses a SOAP Stack and "does their own thing" as I have here, I must perform the SOAP Processing Rules. I've update the code below, changes in RED.
更新:西蒙·费尔(Simon Fell)使我陷入了困境,因为他正确地指出,当人们绕过SOAP堆栈并像我在这里那样“做自己的事”时,我必须执行SOAP处理规则。 我更新了以下代码,更改为红色。
NOTE: In the interest of correctness, I've included namespace qualification in the NEW code.
注意:为了正确起见,我在新代码中包括了名称空间限定。
A fellow emailed me wanting to get a PDF file out of a SOAP Envelope and write it directly out to the browser using Classic ASP. Here's the code I used:
一位同事给我发了电子邮件,希望从SOAP信封中获取PDF文件,然后使用Classic ASP将其直接写到浏览器中。 这是我使用的代码:
<%
Set m_Doc = Server.CreateObject("MSXML2.DOMDocument.4.0")
m_Doc.async = false
m_Doc.ValidateOnParse = false
'This could come from whereever, ADO, a file, another Web Service.m_Doc.Load Server.MapPath(".") + "\\soapresponse.txt" m_Doc.setProperty "SelectionNamespaces", "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:def='http://localhost/'"'There's nothing that I DO understand, so if there's anything I must, I'm screwed.
Set oHeader = m_Doc.selectNodes("//soap:Envelope/soap:Header/*[@soap:mustUnderstand = '1']")
If (Not oHeader Is Nothing) Then
If (oHeader.Length > 0) Then
Response.Write("Crap! I can't continue! What to do?")
Response.End
End If
End If
'Yes, it's a // XPath, but that's the LEAST of our problems before we get into microperf
Set oNode = m_Doc.selectSingleNode("//def:GetImageAsBase64Result")
'This is the Magic that makes it possible. Otherwise you'll get a string.oNode.dataType = "bin.base64"
Response.ContentType="application/pdf"
Response.AddHeader "Content-Disposition", "filename=whatever.pdf"
Response.BinaryWrite oNode.nodeTypedValue
%><% 设置m_Doc = Server.CreateObject(“ MSXML2.DOMDocument .4.0 ”) m_Doc.async = false m_Doc.ValidateOnParse =假'它可能来自ADO,一个文件,另一个Web服务。 m_Doc.Load Server.MapPath(“。”)+“ \\ soapresponse.txt ” m_Doc.setProperty“ SelectionNamespaces”,“ xmlns:soap ='http://schemas.xmlsoap.org/soap/envelope/'xmlns:def ='http:// localhost /'” ``我什么都不懂,所以如果有什么我必须做的,我就被搞砸了。 设置oHeader = m_Doc.selectNodes(“ // soap:Envelope / soap:Header / * [@ soap:mustUnderstand ='1']”) 如果(Not oHeader无效)则如果(oHeader.Length> 0)然后Response.Write(“废话!我无法继续!该怎么办?”) 响应结束万一万一'是的,它是一个// XPath,但这是我们进入microperf前的问题中最不重要的设置oNode = m_Doc.selectSingleNode(“ // def: GetImageAsBase64Result”) '这是使之成为可能的魔术。 否则,您将得到一个字符串。 oNode.dataType =“ bin.base64” Response.ContentType =“ application / pdf” Response.AddHeader“内容处置”,“文件名= whatever.pdf” Response.BinaryWrite oNode.nodeTypedValue %>
This is given a SOAP Response like this:
给出这样的SOAP响应:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetImageAsBase64Response xmlns="http://localhost/">
<GetImageAsBase64Result>JVBERi0xLjI SNIP....etc...<?xml版本=“ 1.0”编码=“ utf-8”?> <soap:信封xmlns:xsi =“ http://www.w3.org/2001/XMLSchema-instance ” xmlns:xsd =“ http://www.w3.org/2001/XMLSchema ” xmlns:soap =“ http ://schemas.xmlsoap.org/soap/envelope/ “> <soap:Body> <GetImageAsBase64Response xmlns =“ http:// localhost / ”> <GetImageAsBase64Result> JVBERi0xLjI SNIP .... etc ...