c# 我应该对用户代理进行空检查吗?

c# Should i put null check for useragent?

我正在将用户代理提取为:

string userAgent = HttpContext.Current.Request.UserAgent;

UserAgent 是否有可能为空?是否只适用于机器人或任何合法的 user/device/client/browser?

User-Agent header 在 RFC 2616 中是可选的。它 SHOULD be used by clients, but it is not REQUIRED to be used. Also see RFC7231:

5.5.3 User-Agent

The "User-Agent" header field contains information about the user agent originating the request, which is often used by servers to help identify the scope of reported interoperability problems, to work around or tailor responses to avoid particular user agent limitations, and for analytics regarding browser or operating system use. A user agent SHOULD send a User-Agent field in each request unless specifically configured not to do so.

认为 RFC7231 废弃了 2616,但为了完整性我都引用了两者。

所以是的,您应该检查是否存在空的或空的用户代理。