繁体   English   中英

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

[英]Add cookie to HTTP header for SOAP call in ColdFusion

我基本上想在通过 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>

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

<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 维护会话状态。

使用上面的链接,我使用基本 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();

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM