Storing and Retrieving Cookie-Based Data

Across the Web, sites make extensive use of “web cookies” to store information about a user on the user’s hard disk. An e-commerce site, for example, might use a cookie to store information about the purchases a user has made in the past. Later, when the user returns to the site, the site’s software can retrieve the cookie-based data and use the data to create a custom page that contains products similar to those the user has purchased in the past. Behind the scenes, the web server and a browser exchange the cookie information. To start, the web server returns the information it wants the browser to store within a cookie file on the user’s disk. Later, when the user revisits the site, the browser, behind the scenes, retrieves the cookie information and sends it to the server.

To create a cookie, an active server page uses the Response object’s Cookie collection. For example, the following statements assign several cookies to a Response object that corresponds to a user’s preferences:

Response.Cookies("Item") = "Shoes" Response.Cookies("Color") = "Brown" Response.Cookies("Price") = "$74.99" Response.Cookies("ShoppingDate") = now()

Likewise, to retrieve the cookie-based data, an active server page references the specific cookie name within the Request object’s Cookie collection. If a variable does not exist, the Request object will return a null value. By testing for the null value, an active server page can easily determine whether or not the user has a cookie on his or her disk from a previous visit.

The active server page in Listing 5.11, CookieDemo.asp, first tests whether or not the user has a cookie defined from a previous visit. If so, the script displays the current cookie settings, updating the value of the MostRecentVisit cookie, as shown in Figure 5.7. If the user did not have a cookie, the page creates and returns one to the user’s browser.

click to expand
Figure 5.7: Displaying cookie-based data within an active server page

Listing 5.11 CookieDemo.asp

start example
 <html> <body> <%   If Request.Cookies("MostRecentVisit") <> "" Then     Response.Write("<h1>Previous Cookie Data</h1>")     Response.Write("Title: " & Request.Cookies("Title") & "<br>")     Response.Write("Publisher: " & Request.Cookies("Publisher") & _ Ä   "<br>")     Response.Write("Author: " & Request.Cookies("Author") & "<br>")     Response.Write("Most recent visit: " & _     Request.Cookies("MostRecentVisit") & "<br>")     Response.Cookies("MostRecentVisit") = now()   Else     Response.Cookies("Title") = "Programming .NET Web Services"     Response.Cookies("Publisher") = "Sybex"     Response.Cookies("Author") = "Jamsa"     Response.Cookies("MostRecentVisit") = now()     Response.Write("Cookie data returned to browser")   End If %> </body> <html>
end example

The problem with the use of cookies is that users can disable their browsers from creating cookies on their hard drive, thus causing cookies to fail.

That said, as you migrate the operations an active server page performs to a web service, there may be times when cookies provide a convenient way for a web service to store data. Further, before a web service can support session data, the web service must support cookies.

Using Cookies within a Web Service

The following CookieDemo web service, for example, creates three cookies that the service then returns to the calling program within the service’s HTTP response. The service supports the GetCookieFields method with a character string:

String GetCookieFields()

To create the CookieDemo web service, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name CookieDemo. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 5.12.

Listing 5.12 CookieDemo.asmx.vb

start example
<WebMethod()> Public Function GetCookieFields() As String     Dim BookCookie As New System.Web.HttpCookie("Book")     Dim PubCookie As New System.Web.HttpCookie("Publisher")     Dim AuthorCookie As New System.Web.HttpCookie("Author")     BookCookie.Value = "Programming Web Services"     Context.Response.AppendCookie(BookCookie)     PubCookie.Value = "Sybex"     Context.Response.AppendCookie(PubCookie)     AuthorCookie.Value = "Kris Jamsa"     Context.Response.AppendCookie(AuthorCookie)     GetCookieFields = "3 Cookies returned" End Function
end example

Within this method, the code creates three cookies variables. The code then assigns a value to each cookie, appending each cookie to the HTTP response message the service returns to the caller.

As discussed, before a client and server can use cookie information, both must support operations. The following Visual Basic .NET program, GetCookie.vb, displays a form (as shown in Figure 5.8) that contains a button upon which the user can click to display the cookie information set by the CookieDemo web services. Some clients, for example, might store the cookie values on the user’s hard disk so the client can later retrieve and send the cookies to the web service the next time the client calls the service (much as a browser saves and later recalls cookies).

To create the GetCookie.vb program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.


    Figure 5.8: Displaying cookies set by a remote web service

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click Windows Application. Within the Name and Location fields, type GetCookie. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls.

  3. Using the Toolbox, drag and drop the text box and button previously shown in Figure 5.8 onto the form.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type localhost/CookieDemo/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 5.13.

Listing 5.13 GetCookie.vb

start example
Private Sub Button1_Click(ByVal sender As System.Object, _ Ä   ByVal e As System.EventArgs) Handles Button1.Click     Dim WS As New localhost.Service1()     Dim Cookies As New CookieContainer()     Dim CookiesReturned As New CookieCollection()     WS.CookieContainer = Cookies     Try         TextBox1.Text = WS.GetCookieFields()         Dim Uri = New Uri(WS.Url)         CookiesReturned = WS.CookieContainer.GetCookies(Uri)         Dim Entry As Cookie         For Each Entry In CookiesReturned             TextBox2.Text = TextBox2.Text & Entry.Name _                & " " & Entry.Value & vbCrLf         Next     Catch Ex As Exception         TextBox2.Text = Ex.Message     End Try End Sub
end example

The program first creates the Cookies variable that corresponds to a cookie container the program will pass to the service, into which the service can assign cookies and their corresponding values. The program then assigns the cookie container to the web-service object, so the cookie container is passed to the remote service. Later, after the program calls the remote method, the code loops through each of the cookies returned by the service.




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net