在尝试使用ColdFusion使用SOAP API时遇到了麻烦

在尝试使用ColdFusion使用SOAP API时遇到了麻烦,soap,coldfusion,Soap,Coldfusion,我正在使用一个API,它有两个不同的URL用于特定类型的函数。第一个是支持JSON或SOAP请求的事务API。我已经使用专门的JSON调用调用了这个API中需要的所有函数,所有功能都运行得非常好 第二个是用于查找和/或下载报告的报告API。此API仅适用于SOAP。我无法使此API中的任何函数正常工作。我曾试图联系该公司的支持小组,但他们没有任何人可以帮助我在ColdFusion中调用API。我尝试了两种不同的方法来与这个API接口并访问函数,但结果都是空的。以下是我的例子和尽可能多的信息;我们

我正在使用一个API,它有两个不同的URL用于特定类型的函数。第一个是支持JSON或SOAP请求的事务API。我已经使用专门的JSON调用调用了这个API中需要的所有函数,所有功能都运行得非常好

第二个是用于查找和/或下载报告的报告API。此API仅适用于SOAP。我无法使此API中的任何函数正常工作。我曾试图联系该公司的支持小组,但他们没有任何人可以帮助我在ColdFusion中调用API。我尝试了两种不同的方法来与这个API接口并访问函数,但结果都是空的。以下是我的例子和尽可能多的信息;我们服务提供商的API和相关文档是保密的,但我可以回答一些与我的代码相关的问题

方法1:创建webservice对象

我尝试创建此SOAP调用的第一种方法是通过webservice对象。使用元数据交换点URL,我将其传递到createObject函数中,如下所示:

<cfset argStruct = structNew() />
<cfset argStruct['username'] = 'myusername' />
<cfset argStruct['password'] = 'mypassword' />
<cfset testSvc = createObject('webservice','https://brandnameapi.sandbox.serviceprovider.com/vernum/ReportingAPI.svc/mex',argStruct) />

运行此代码时,会收到以下错误消息:

无法为web服务调用生成存根对象。名称:。WSDL:。javax.wsdl.WSDLException:WSDLException(at/wsdl:definitions/wsdl:import):faultCode=OTHER_错误:无法解析位于“”的导入文档https://brandnameapi.sandbox.serviceprovider.com/vernum/ReportingAPI.svc/mex?wsdl=wsdl1',相对于“brandnameapi.sandbox.serviceprovider.com/vernum/ReportingAPI.svc/”:java.io.IOException:服务器返回了URL的HTTP响应代码:401:

由于401错误通常是不正确的授权,我通过直接在浏览器中调用URL对地址进行了双重检查,在浏览器中我得到了一个UN/PW提示。我输入了我的值,并被允许访问URL,并收到以下XML作为回报:

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions name="ReportingAPI" targetNamespace="https://https://brandnameapi.serviceprovider.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:tns="https://https://brandnameapi.serviceprovider.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:i0="https://https://brandnameapi.serviceprovider.com/ReportingAPI/soapBinding" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<wsdl:import namespace="https://https://brandnameapi.serviceprovider.com/ReportingAPI/soapBinding" location="https://https://brandnameapi.sandbox.serviceprovider.com/vernum/ReportingAPI.svc/mex?wsdl=wsdl1"/>
<wsdl:types/>
<wsdl:service name="ReportingAPI">
    <wsdl:port name="BasicHttpBinding_IReportingAPI" binding="i0:BasicHttpBinding_IReportingAPI">
        <soap:address location="https://https://brandnameapi.sandbox.serviceprovider.com/vernum/ReportingAPI.svc/soap"/>
    </wsdl:port>
</wsdl:service>

这是我所能做到的。当我用浏览器调用URL并传递身份验证信息时,我可以访问XML。当我尝试使用ColdFusion执行此操作时,会出现401个错误

方式2:cfhttp请求调用

当我转而使用cfhttp时,我似乎更进一步了。当我使用此选项时:

<cfhttp url="https://brandnameapi.sandbox.serviceprovider.com/vernum/ReportingAPI.svc/mex"
username="myusername" password="mypassword" method="get" result="httpResponse" 
timeout="300">
</cfhttp>

httpResponse返回适当的页面信息,httpResponse.filecontent返回的XML与我在浏览器中直接调用它时收到的XML相同

更进一步,我使用SOAP URL并尝试调用API中的函数,该函数返回可用报告文件的列表。我使用了与事务API中所有JSON调用相同的已知工作流程:

<cfhttp url="https://brandnameapi.sandbox.serviceprovider.com/vernum/ReportingAPI.svc/soap/queryAvailableReportFiles"
username="myusername" password="mypassword" method="post" 
result="httpResponse" timeout="300">
<cfhttpparam type="formfield" name="typeOfReport" value="DailyCSVFile" />
</cfhttp>

当我运行此代码时,我得到状态代码415和错误“无法处理消息,因为内容类型”application/x-www-form-urlencoded“不是预期的类型”multipart/related;type=“应用程序/xop+xml”

当我在cfhttps之间添加此行时:

<cfhttpparam type="header" name="Content-Type" value='multipart/related; type="application/xop+xml"' />

我得到了与上面相同的错误,但是状态代码变为400。我并没有把我想做的每件事都包括进去,只包括我现在所处的位置。我将回答尽可能多的问题,并将按照指示重新制定步骤,以找到解决此问题的方法

更新:根据请求,我已将cfhttp调用更改为尝试传递XML,而不是表单字段。我的XML代码直接取自API文档,该文档中有一个原始数据示例,用于不同函数的API请求:

<cfsavecontent variable="soapBody">
<cfoutput>
    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
        <s:Body>
            <queryAvailableReportFiles
xmlns="https://brandnameapi.sandbox.serviceprovider.com/contract">
                <fileName>DailyCSVFile</fileName>
            </queryAvailableReportFiles>
        </s:Body>
    </s:Envelope>
</cfoutput>
</cfsavecontent>

<cfhttp url="https://prismproapi.sandbox.koretelematics.com/4/ReportingAPI.svc/soap/queryAvailableReportFiles" username="vfapi" password="bPzqQyK3" method="post" result="httpResponse" timeout="300">
<cfhttpparam type="xml" value="#trim(soapBody)#" />
</cfhttp>

每日档案

公平地说,我不知道我做得对不对。从中返回的错误消息是:“由于EndpointDispatcher的AddressFilter不匹配,收件人无法处理带有To的消息。请检查发件人和收件人的EndpointAddresses是否一致。”我还收到一个500错误。

以下是我通常如何编写和执行SOAP请求的示例。请注意,您需要修改SOAP主体以满足API的需要。希望这能帮助你朝着正确的方向前进

顺便说一下,本·纳德尔有一个出色的右上角

以下是我的示例代码:

<!--- Compose SOAP message to send to Web Service --->
<cfsavecontent variable="soapRequest">
    <?xml version="1.0" encoding="UTF-8" ?> 
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://www.domain.com/soap/example/">
        <soapenv:Header/>
        <soapenv:Body>
          <example:ReportAPI>
             <typeOfReport>DailyCSVFile</typeOfReport>
          </example:ReportAPI>
        </soapenv:Body>
    </soapenv:Envelope>
</cfsavecontent>

<!--- Send SOAP request to the Web Service --->
<cfhttp url="https://brandnameapi.sandbox.serviceprovider.com/vernum/ReportingAPI.svc/soap/queryAvailableReportFiles" username="myusername" password="mypassword" method="post" result="httpResponse" timeout="300">
    <cfhttpparam type="header" name="content-type" value="text/xml" />
    <cfhttpparam type="header" name="content-length" value="#Len(Trim(soapRequest))#" />
    <cfhttpparam type="header" name="charset" value="utf-8" />
    <cfhttpparam type="xml" name="message" value="#Trim(soapRequest)#" />
</cfhttp> 

每日档案

他们的API有问题。没有代码更改可以解决此问题。

您是否尝试使用此web服务?每当我遇到SOAP问题时,我总是使用SoapUI进行测试。如果你能让它工作,那么它可能会指出什么是ColdFusion所需要的。此外,我总是使用
cfhttp
来进行web服务调用。我相信它可以让您更好地控制大多数web服务所需的配置方面。报告web服务是否需要XML内容?如果将wsdl附加到createobject()中的url,会发生什么情况?@Miguel-F,我没有使用过SoapUI。我以前从没听说过。文档没有说明预期的内容类型,因此我必须假设该内容类型是指定为预期内容的内容类型。@DanBracuk-将?wsdl添加到URL末尾会生成相同的错误消息。SoapUI是使用SOAP服务的一个很好的工具。您看到我评论的另一部分了吗?服务是否需要XML内容?您的上一个请求似乎已关闭,但以400失败,好像它在期待什么。我通常必须构建xml SOAP请求,然后通过
cfhttp
调用发送它。但是您需要知道服务需要什么才能做到这一点。我修改了我的cfhttp调用,以使用文档提供的所有头值,并继续得到相同的错误rel