Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use HttpBrowserCapabilities from a c# console application?

I need to parse UserAgent strings from a console app and this seems like a simple way to do it, but I obviously don't have an HttpRequest object and can't seem to make a fake one with a User-Agent header (I get platform not supported exception). Is there any way to do this, or should I start exploring other alternatives to user agent parsing?

like image 376
Jody Powlette Avatar asked Oct 08 '10 14:10

Jody Powlette


1 Answers

The User-Agent header can be parsed by the HttpBrowserCapabilities class with the help of a BrowserCapabilitiesFactory, as follows:

var userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.10) " +
                "Gecko/20100914 Firefox/3.6.10";
var browser = new HttpBrowserCapabilities {
    Capabilities = new Hashtable {{string.Empty, userAgent}}
};
var factory = new BrowserCapabilitiesFactory();
factory.ConfigureBrowserCapabilities(new NameValueCollection(), browser);
like image 146
Nathan Baulch Avatar answered Oct 06 '22 03:10

Nathan Baulch