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



当通过ColdFusion调用SOAP web服务时,我基本上想将ASP.NET_SessionId cookie添加到我的HTTP请求头中。

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>

我的web服务被这样调用:

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

为了让。net服务器(web服务所在的地方)记住我的会话状态,我必须在HTTP请求头中传递ASP.NET_SessionId cookie,但不知道这是否甚至可能在ColdFusion中。

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

短答:

对于CF9/Axis1,尝试在web服务对象上启用会话。

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

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


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

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

  • 如何在ColdFusion SOAP请求中设置cookie -(大约2006年)- 为CF/Axis的旧版本编写,因此有些内容已经过时了,但它仍然提供了一个很好的总体概念概述。

  • 维护Axis2中的会话

  • Java axis web service client setMaintainSession on multiple services (cookies?)

  • Axis2手动管理会话Cookie

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

// 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();

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

在CFHPPT标签中,使用CFHTTPPARAM设置cookie

最新更新