在 ColdFusion 中为 SOAP 调用添加 cookie 到 HTTP header

Add cookie to HTTP header for SOAP call in ColdFusion

我基本上想在通过 ColdFusion 调用 SOAP Web 服务时将 ASP.NET_SessionId cookie 添加到我的 HTTP 请求 header。

Web 服务在 ColdFusion 的 Application.cfc 组件的 OnApplicationStart 函数中注册。

<cfscript>
    objSoapHeader = XmlParse("<wsse:Security mustUnderstand=""true"" xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd""><wsse:UsernameToken><wsse:Username>MY_USERNAME</wsse:Username><wsse:Password>MY_PASSWORD</wsse:Password></wsse:UsernameToken></wsse:Security>");
    Application.UserWebService = CreateObject("webservice","MY_URL/UserService.asmx?WSDL");
    addSOAPRequestHeader(Application.UserWebService,"","",objSoapHeader,true);
</cfscript>

我的网络服务是这样调用的:

<cfset Result = "#Application.UserWebService.SomeFunction("1", "DATA")#">

为了让 .Net 服务器(Web 服务所在的位置)记住我的 session 状态,我必须在 HTTP 请求 header 中传递 ASP.NET_SessionId cookie,但不知道这在 ColdFusion 中是否可行。

我已经研究了几个小时,但到目前为止还没有任何结果,所以有人成功地完成了吗?

在您的 CFHPPT 标签内,使用 CFHTTPPARAM

设置 cookie

简答:

对于 CF9/Axis1,请尝试在 Web 服务 object 上启用 sessions。

ws = createObject("webservice", "http://localhost/MyWebService.asmx?wsdl");
ws.setMaintainSession( true );

对于 CF10+ / Axis2,请参阅下面更长的答案:


(免责声明:使用 cfhttp 可能更简单,但我很好奇并做了一些挖掘..)

据我所知,由于 CF10+ 使用 Axis2 作为 Web 服务,因此应该可以使用底层方法通过 HTTP cookie 维护 session 状态。

使用上面的链接,我使用基本的 Web 服务组合了一个快速的 POC,并且能够从 Web 服务客户端响应中提取 cookie header:

// make initial request
ws = createObject("webservice", "http://localhost/MyWebService.asmx?wsdl");
ws.firstMethod();

// For maintainability, use constants instead of hard coded strings
wsdlConstants = createObject("java", "org.apache.axis2.wsdl.WSDLConstants");

// Extract headers 
operation = ws._getServiceClient().getLastOperationContext();
context = operation.getMessageContext( wsdlConstants.MESSAGE_LABEL_IN_VALUE  );
headers = context.getProperty( context.TRANSPORT_HEADERS );

然后设置 cookie 并指示 Web 服务客户端将其与后续请求一起发送:

if ( structKeyExists(headers, "Set-Cookie") ) {
    // include http cookies with request
    httpConstants = createObject("java", "org.apache.axis2.transport.http.HTTPConstants");
    options = ws._getServiceClient().getOptions();
    options.setManageSession( true );
    options.setProperty( httpConstants.COOKIE_STRING, headers["Set-Cookie"] );
}

// ... more requests
ws.secondMethod();
ws.thirdMethod();

注意: 旁注,我注意到您正在将实例存储在共享应用程序范围内。请记住,Web 服务实例可能不是线程安全的。