2012-03-31 70 views
5

我有一个自托管的WCF应用程序,它使用基本HTTP绑定(无SSL),在.NET Framework 4.0的控制台应用程序中运行。WCF WebGet Capture HTTP Referrer?

我有一个方法的WebGet属性返回一个人类可读的字符串作为“烟雾测试”。

如果我有一个ASP.NET webforms页面,我会使用Request.UrlReferrer或ServerVariables(“HTTP_REFERER”)来查看客户端是否志愿他们的重定向信息。

我怎么用WCF来做到这一点?

谢谢。

回答

5

如果您使用的是BasicHttpBinding,则WebGet属性可能被忽略(它用于使用webHttpBindingWebHttpBehavior的端点)。

如果您使用的是“web”端点(WebHttpBinding/WebHttpBehavior),则可以使用WebOperationContext.Current.IncomingRequest.Headers[HttpRequestHeader.Referer]。如果您没有对System.ServiceModel.Web.dll的引用,则还可以使用OperationContext中的HttpRequestMessageProperty

HttpRequestMessageProperty prop; 
prop = (HttpRequestMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name]; 
var referer = prop.Headers[HttpRequestHeader.Referer]