2016-09-15 82 views
1

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

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服务的位置)记住我的会话状态,我必须通过ASP.NET_SessionId的cookie中HTTP请求头,但不知道这是否甚至可以在ColdFusion中使用。

我已经研究了好几个小时,但是到目前为止还没有成功,所以有人成功设法解决这个问题?

+0

这就是你如何在cfhttp中设置cookie。 ''。但我不知道从哪里获取.NET值。你有没有试过用''查看cgi范围? – Jules

+0

为了澄清,这里的建议是尝试使用'cfhttp'而不是'createObject()'来使用webservice。在某些情况下,它提供了更多的灵活性。请参阅此主题以了解有关如何提取Cookie并通过http请求维护会话的详细信息:http://www.bennadel.com/blog/725-maintaining-sessions-across-multiple-coldfusion-cfhttp-requests.htm – Leigh

+0

@Leigh - 他正在寻找.NET会话ID,而不是CF。但我同意你的意见。 cookie.CFID或cookie.JSESSIONID可能更好用。 – Jules

回答

1

简答饼干:

对于CF9 /轴1尝试Web服务对象实现会话。

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

对于CF10 +/Axis2中,看到不再回答以下:


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

从我读过的内容来看,由于CF10 +使用Axis2作为Web服务,因此应该可以使用底层方法通过HTTP Cookie维护会话状态。

使用上面的链接,我放在一起使用基本的Web服务的快速POC,并能够提取的cookie来自Web服务客户端的响应:

// 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服务实例可能不是线程安全的。

+0

非常有趣!我今天会研究这个,但是我正在运行ColdFusion 9,因此我相信我会使用Axis1 – MPaul

+0

(编辑)@MPaul - 那么您可以使用第一个链接中引用的技术。我认为它是为Axis1编写的。尽管CF的早期版本(7或类似的东西),所以不保证所有这些将适用于您的版本;-) – Leigh

+1

[axis1 syntax here](http://axis.8716.n7.nabble.com /Axis-1-4-client-get-cookies-td23763.html)似乎适用于CF9。出于好奇,如果启用会话会发生什么,例如'ws.setMaintainSession(true);'?它是否仍然失去了会议(在CF9下)? – Leigh