Java Reference
In-Depth Information
Table 4.1: HTTP Request Header Methods and Functions
Method or Function Name
Purpose
addRequestProperty(String key, String value)
Adds a general request property
specified by a key-value pair.
getRequestProperties()
Returns an unmodifiable Map of
general request properties for this
connection.
getRequestProperty(String key)
Returns the value of the named
general request property for this con-
nection.
setRequestProperty(String key, String value)
Sets the general request property.
Usually the only method from the above list that you will use will be the
setRequestProperty method. The others are useful when you need to query what
values have already been set. If there is already a header with the specified name, then
setRequestHeader will overwrite it. The addRequestProperty can be used
to add more than one of the same request header with the same name. Usually, you do not
want to do this. Adding more that one header of the same name is useful when dealing with
cookies - which are discussed in Chapter 8, “Handling Cookies and Sessions”.
Identifying the Browser Type
One of the HTTP request headers identifies the browser type that the user is using. Many
web sites take this header in to account. For example, some web sites are only designed to
work with certain versions of Microsoft Internet Explorer. To make use of such sites, you
need to change how HttpURLConnection reports the browser type.
The browser type can be determined from the user-agent HTTP request
header. You can easily set the value of this, or any, HTTP request header using the
setRequestProperty . For example, to identify the bot as a browser of type “My
Bot”, you would use the following command:
http.setRequestProperty("user-agent","My Bot");
The user-agent header is often used to identify the bot. For example, each of the
major search engines use spiders to find pages for their search engines. These search engine
companies use user-agent headers to identify them as a search engine spider, and not
a human user.
When you write a bot of your own, you have some decisions to make with the
user-agent header. You can either identify the bot, as seen above, or you can emulate
one of the common browsers. If a web site requires a version of Internet Explorer, you will
have to emulate Internet Explorer.
Search WWH ::




Custom Search