如何在ColdFusion中向客户端发送HTTP状态代码和响应消息? [英] How can I send HTTP Status Code and a Response messages to the client in ColdFusion?
问题描述
我正在实现单一登录功能。我有一个ColdFusion应用程序,它从Java应用程序(POST请求)获取输入参数。我需要做的是返回状态代码和说明,以指示用户是否可以访问以及如果用户没有访问我的CF应用程序的失败原因。
类似于以下内容:
I am implementing the Single Sign On functionality. I have an ColdFusion application which takes input parameters from Java application (POST request). What I need to do is return status codes and a description to indicate whether the user has access and the failed reason if the user does not have access to my CF application. Something like below:
我创建了一个cfc并将其作为API提供允许Java用户将其UserName,CustomerID传递给我的CF应用程序。
是否需要在同一文件中编写返回响应逻辑?就像抛出错误代码(cfthrow)的函数一样。
I have created a cfc and provided this as an API to allow Java users to pass in their UserName, CustomerID to my CF application. Do I need to write the return response logic in the same file? Like a function which "throw" error code (cfthrow).
或者我可以使用 cfheader...。类似这样:
Or may be I can use "cfheader"....something like this:
<cfif form.CustomerId EQ queryname.CustID>
<CFHEADER
STATUSCODE="200"
STATUSTEXT="Success">
<cfelse>
<CFHEADER
STATUSCODE="400"
STATUSTEXT="Insufficient Input">
</cfif>
有人可以在这里帮助我吗?
Can anyone please help me here?
推荐答案
您可以使用:
component restpath = "your/rest/path" rest="true"
{
remote void function errorTest()
httpmethod = "GET"
restpath = ""
{
cfheader(
statuscode = 401,
statustext = "Invalid Password"
);
// or
restSetResponse({
status = 401,
headers = { explanation = "Invalid Password" }
});
// or, using Java
getPageContext()
.getResponse()
.getResponse()
.sendError( JavaCast( 'int', 401 ), "Invalid Password" );
// or, using the deprecated setStatus(int,string) method in Java
getPageContext()
.getResponse()
.getResponse()
.setStatus( JavaCast( 'int', 401 ), "Invalid Password" );
}
}
注意:我没有找到方法可以直接使用 restSetResponse()
设置消息,从而返回带有消息的自定义标头。
Note: I have not found a way to directly set the message using restSetResponse()
so this returns a custom header with the message instead.
这篇关于如何在ColdFusion中向客户端发送HTTP状态代码和响应消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!