Java Reference
In-Depth Information
HTTP/1.1 302 Found
Date: Thu, 31 Aug 2006 23:39:07 GMT
Server: Apache/2.0.40 (Red Hat Linux)
Accept-Ranges: bytes
X-Powered-By: PHP/4.3.2
Set-Cookie: hri-cookie=42252vvf5x4d5d69xq1ub653ltnh9xo7
Location: menuc.php
Content-Length: 0
Connection: close
Content-Type: text/html
Notice the cookie? When the server wants to create a cookie, the server returns the
Set-Cookie header. This specifies that the cookie named hri-cookie has the value
42252vvf5x4d5d69xq1ub653ltnh9xo7 .
This tells the browser to always include this cookie in future requests. For example, if
you were to perform a search while logged in, your HTTP POST request would look like
this:
POST /1/8/menuc.php HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, ap-
plication/x-shockwave-flash, */*
Referer: http://www.httprecipes.com/1/8/menuc.php
Accept-Language: en-us
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;
SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
Host: www.httprecipes.com
Content-Length: 32
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: hri-cookie=42252vvf5x4d5d69xq1ub653ltnh9xo7
search=Miss&type=s&action=Search
As you can see, the browser is now sending the cookie back as a request header. When
the server requests a cookie, the Set-Cookie header is used; when the browser sends
this cookie back, the Cookie header is used.
Processing Cookies
The recipes in this chapter will make use of a simple class named CookieUtility
to process cookies. Any recipe in this topic that makes use of cookies will also use this
class. In this section, you will see how the CookieUtility class was constructed. The
CookieUtility class is shown in Listing 8.1.
Search WWH ::




Custom Search