Sunday, August 31, 2014

How to get the user-agent with C#?

Web api?


It's stored in the webheadercollection.


You could try:



if (Request.Headers.Contains("User-Agent"))
{
var headers = request.Headers.GetValues("User-Agent");

StringBuilder sb = new StringBuilder();

foreach (var header in headers)
{
sb.Append(header);

// Re-add spaces stripped when user agent string was split up.
sb.Append(" ");
}

userAgent = sb.ToString().Trim();
}





Hope that helps

Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.


No comments:

Post a Comment