www.developer.com/net/article.php/2222811
|
June 16, 2003 IntroductionWelcome to Web Service Secrets! I'm Karl Moore and this is the twelfth and final installment of the .NET secrets series, the bundle of articles based on my latest book, VB.NET and ASP.NET Secrets. Today, we'll be exploring a bundle of Web service tips and samples to wire your applications, including:
Of course, these are only teasers—you'll find full versions, plus hundreds more such tricks in the book. But you'll need to cough up a few dollars first. Go on, don't be mean. Ready to begin exploring those nifty little Web service secrets? Top stuff... The Secrets!Five Things You Need to Do Before Publicizing Your Web ServiceSo, you've written the greatest Web service since SlicedBread.asmx, and now you want to tell the whole world about your creation? Well, before you do, ensure you follow this list of five quick changes that will give your service that professional touch:
Improving Performance with Easy CachingIt's a little-known fact, but you can actually "cache" what a Web service gives out and automatically serve up the same response next time, seriously speeding up popular, processor-intensive services. How? Simply specify a CacheDuration parameter in the WebMethod attribute, like this:
<WebMethod(CacheDuration:=60)> _
Public Function GetWeather() As String
' ... complicated code ...
End Function
Here, the Web server will cache and serve up the same results for GetWeather sixty seconds after the last query. You can check out this cute tip in action by returning the time from a function. See what happens? Adding Google Search to Your ProgramsIt started off as "that nerdy site with the funny name" and ended up as the world's number one search engine. Yes, it's Google at www.google.com—an intelligent, easy-to-use searchable archive of over two billion Web pages. And thanks to the world of Web services, you can plug your application into this mass of knowledge—with just a few lines of code. How? First off, you'll need to check out www.google.com/apis/ for information on the process. The main thing you'll want to do here it create a Google account. You'll be asked for your e-mail address and other details—in return, you'll be provided with a license key. You need to use this when accessing the service. Got your license key? Then fire up Visual Studio .NET and create a new project, such as a Windows application. Select Project > Add Web Reference and type in the URL of the Google .WSDL (Web Service Definition Language) file—http://api.google.com/GoogleSearch.wsdl as listed on the Google site—then press Return. Click 'Add Reference' when the button becomes available.And now? Simply start using the service! To get you started, let's review a little sample code. Here's my own attempt at performing a simple search, returning a maximum of ten items for the term 'Karl Moore' (I know, narcissistic):
' Create a new Google search object
Dim objSearch As New _
com.google.api.GoogleSearchService()
' Invoke the search method
Dim objResults As _
com.google.api.GoogleSearchResult = _
objSearch.doGoogleSearch( _
"license-key-goes-here1Ga0szyTgT0K3r2Oq5pAMSvX1SYGvNYl", _
"Karl Moore", 0, 10, False, "", False, _
"", "", "")
' Loop through result elements
Dim shtCount As Short
For shtCount = 0 To objResults.endIndex - 1
MessageBox.Show(objResults.resultElements(shtCount).title & _
" - " & objResults.resultElements(shtCount).URL)
Next
Top Tip: The Google service will include <b> bold tags around text that you search for. This doesn't matter if you're developing for Web applications, where highlighting searched-for terms is actually quite user friendly. For Windows applications, however, that don't use HTML tags, you'll want to strip these out using a custom algorithm or regular expression (check out my "Converting HTML to Text, Easily" tip in the "More .NET Secrets" chapter of my book, VB.NET and ASP.NET Secrets for a ready-to-run algorithm). So, that's a regular search. But there's more. Regular users of Google will know that when you accidentally mistype a search term, the engine will suggest a possible correct spelling. You can access that exact functionality through your Web service too, as so:
' Create a new Google search object
Dim objSearch As New _
com.google.api.GoogleSearchService()
' Retrieve the suggest spelling, if any
Dim strSuggestion As String
strSuggestion = objSearch.doSpellingSuggestion( _
"license-key-goes-here1Ga0szyTgT0K3r2Oq5pAMSvX1SYGvNYl", _
"Britnee Speyers")
MessageBox.Show(strSuggestion)
Thought that was it? You also can retrieve cached versions of a page (alongside the Google "this is a cached page, click here for the latest version, etc." HTML). Here's a little sample code:
' Create a new Google search object
Dim objSearch As New _
com.google.api.GoogleSearchService()
' Retrieve cached version of a page
Dim bytPage() As Byte = _
objSearch.doGetCachedPage( _
"license-key-goes-here1Ga0szyTgT0K3r2Oq5pAMSvX1SYGvNYl", _
"www.karlmoore.com")
' Convert base 64 byte array to string
Dim strHTML As String = _
System.Text.ASCIIEncoding.ASCII.GetString(bytPage)
MessageBox.Show(strHTML)
Here, I've simply demonstrated the three core features of the Web service; however, by downloading the Developer's Toolkit above, you can really delve into any of these areas. By filling out a few of the extra . doGoogleSearch parameters, for example, you can restrict results to a particular language, search within a particular topic, or alter the number of returned results. Be sure to check it out. Also, it's worthwhile noting that at the time of writing, the Google Web service was still in beta. It is, however, unlikely that Google will change the interface definitions (a prediction based on the way it has continued to keep certain redundant properties available, even though still in beta—a sure sign of a user-conscious organization, if not one lacking in a little foresight). In addition, be aware that (again at the time of writing) you are limited to 1,000 searches per day, per license key and that no commercial pay-per-search facility is yet available.
Plugging directly into the power that is Google, through Web services |