Web services 将cookie添加到ColdFusion中SOAP调用的HTTP头中

Web services 将cookie添加到ColdFusion中SOAP调用的HTTP头中,web-services,cookies,coldfusion,httprequest,coldfusion-9,Web Services,Cookies,Coldfusion,Httprequest,Coldfusion 9,当通过ColdFusion调用SOAP web服务时,我基本上希望将ASP.NET_SessionIdcookie添加到我的HTTP请求头中 <cfscript> objSoapHeader = XmlParse("<wsse:Security mustUnderstand=""true"" xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0

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

<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服务在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>

objSoapHeader=XmlParse(“我的用户名我的密码”);
Application.UserWebService=CreateObject(“webservice”,“MY_URL/UserService.asmx?WSDL”);
addSOAPRequestHeader(Application.UserWebService,“,”,objSoapHeader,true);
我的web服务被称为:

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

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

<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>

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

在您的CFHPPT标签中,用简短回答设置cookie:

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

ws = createObject("webservice", "http://localhost/MyWebService.asmx?wsdl");
ws.setMaintainSession( true );
对于CF10+/Axis2,请参阅下面的详细答案:


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

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

  • -为CF/Axis的旧版本编写,因此其中一些已经过时,但它仍然提供了对一般概念的良好概述

使用上面的链接,我使用基本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服务实例可能不是线程安全的

这是在cfhttp中设置cookie的方式<代码>。但我不知道从哪里可以得到.NET的价值。您是否尝试过使用
查看cgi范围?为了澄清,这里的建议是尝试使用
cfhttp
而不是
createObject()
使用Web服务。在某些情况下,它提供了更大的灵活性。有关如何提取Cookie和跨http请求维护会话的详细信息,请参阅此线程:@Leigh-他正在查找.NET会话id,而不是CF。但我还是同意你的看法。cookie.CFID或cookie.JSESSIONID可能更适合使用。@Jules-是的,我知道;-)。。但是我想你误解了我的建议。@Jules为了回答你的第一个评论,我从
GetHttpRequestData().Headers.cookie
中检索了
ASP.NET_SessionId
cookie并将其拆分为一个数组。虽然不是最理想的方法,但这允许我检索.Net返回的cookie,以便在所有后续请求中识别正确的SessionState实例。非常有趣!今天我将对此进行研究,但是我运行的是ColdFusion 9,因此我相信我将使用Axis1(Edit)@MPaul-然后您可能可以使用第一个链接中引用的技术。我认为它是为Axis1编写的。虽然是CF的早期版本(7或类似的版本),但不能保证所有这些都适用于您的版本;-)这似乎适用于CF9。出于好奇,如果只启用会话,会发生什么,即
ws.setMaintainSession(true)?它是否仍然会丢失会话的轨道(根据CF9)?很高兴听到!对于Axis2,在早期的测试中,仅仅设置
setManageSession
似乎不起作用,但现在我想知道。。。我得重新检查一下。