如何检测 Web 服务中的用户代理?我的 Web 服务是使用带有 basicHTTPBinding 的 WCF Web 服务实现的。这将是来自一些 SOAP 客户端的帖子。我想了解客户的用户代理。
我希望看到一些示例代码。
我正在使用基于 WCF 的 Web 服务,在 svc.cs 中,我尝试捕获this.Context.Request.UserAgent
。但它给出了以下错误:
this.Context.Request.UserAgent 'MySoapService.MyService' does not contain a definition for 'Context' and no extension method 'Context' accepting a first argument of type 'MySoapService.MyService' could be found (are you missing a using directive or an assembly reference?)
我也尝试过System.Web.HttpContext.Current.Request.UserAgent
它说:
'System.Web.HttpContext.Current' is null
编辑注:
我尝试激活 ASP.NET 兼容模式。我添加了<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
在配置文件中并添加[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
在实现服务接口的类的顶部。然后使用 System.Web.HttpContext.Current.Request.UserAgent 为我提供所需的用户代理。
还有另一种方法可以在不启用 web.config 中的 ASP.NET 兼容性的情况下获取用户代理:
string userAgent = WebOperationContext.Current.IncomingRequest.Headers["User-Agent"];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)