views:

193

answers:

3

How do websites find out which browser is visiting them

how i can do this

Are you give answer for asp.net c#

+9  A: 

They look for the user agent passed in the request.

In ASP.NET:

Request.ServerVariables["HTTP_USER_AGENT"]

antik
Wouldn't you use square brackets here? Request.ServerVariables["HTTP_USER_AGENT"]
Jeremy
@Jeremy Fixed. Good catch.
antik
+2  A: 

The HTTP protocol provides an attribute of the request header called the User-Agent which the client (here the web browsers) fill-in with a string identifying the browser make, version and operating system. Like all elements of the HTTP header, this information may well be "spoofed" or altered for various purposes (for example by various client-side privacy gateways and such), but it is usually relatively reliable.

An example of such a User-Agent string is (here for a FireFox browser, Version 3.5, running under Windows XP)

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5

This information, along with other attributes from the header can be queried by the receiving application. Although the specifics vary from one language/framework to the next, may of these languages/framworks expose a simple object model which mirrors the various objects associated with the HTTP protocol. In the case of the http header, this typically comes from the "Request" (may be named differently) object, so accessing the User-Agent may look something like:

  ClientBrowser = Request.Header("User-Agent")

or possibly

  ClientBrowser = HttpHeader.UserAgent

Edit: In the case of C#/ASP.NET (late edit of question):

ClientBrowser = Request.ServerVariables("HTTP_USER_AGENT")

Also, although you may be tempted to use this information directly, you may also rely on various libraries which encapsulate the details of parsing the [very many versions of the] User-Agent strings to figure out the particular web browser and even the particular forms of javascript such client should be sent.

mjv
+3  A: 

The browser tells the server what kind of browser it is in the User-Agent string, which it includes with each HTTP request.

You can access the User-Agent directly and parse it yourself, or you can use ASP.NET's built-in browser capabilities feature, which relies on several *.browser files, regular expressions, etc.

User-Agent: <%= Request.UserAgent %>
ID: <%= Request.Browser.Id %>
Browser: <%= Request.Browser.Browser %>
Type: <%= Request.Browser.Capabilities["type"] %>
RickNZ
Very very good answer. so are you write it in detailed
Are you write any answer in detailed