I want to log the user agents information in my log file. So I used this code:
System.Web.HttpContext.Current.Request.UserAgent;
It works fine, when I'm using the browser to consume my web service (the user agent returns the right value) but sometimes the above code doesn't work.
for example when i use the WCF test client user agent string is null in my log file but in IIS log file has this value: Mozilla/4.0+(compatible;+MSIE+6.0;+MS+Web+Services+Client+Protocol+4.0.30319.34003)
i also tried these codes:
OperationContext context = OperationContext.Current;
HttpRequestMessageProperty httpRequest =
context.IncomingMessageProperties["httpRequest"] as HttpRequestMessageProperty;
if (httpRequest != null)
{
string userAgentInfo = httpRequest.Headers["User-Agent"];
}
and
string userAgent = WebOperationContext.Current.IncomingRequest.Headers["User-Agent"];
I guess I miss something here but I don't now what. i really appreciated if you can help me to log the complete user agents information in my log file. thank you.
Its a massive mess and not something that can be decoded easily.
Because of some scripts in the early days of the web which used to sniff the browser version and reject sites that werent compatible a lot of the browsers started reporting crazy stuff just to get around it. Netscape claimed to be IE and i think IE claimed to be netscape and well its all a mess. Plus its possible to send whatever you want in that string so its not guaranteed to be correct even if the string does make sense.