Using vb.net to create a Web service access program

Source: Internet
Author: User
Web|web Services | programs | create | access

A Introduced:

Web Services is an internet-based development model proposed by Microsoft, which has received wide push from the industry. From the current trend of development, Web services will lead the next generation of internet trends. It is a new platform for creating interoperable distributed applications, and is a network-based, distributed modular component. It performs specific tasks and adheres to specific technical specifications that enable Web services to interoperate with other compatible components.

This article will show you how to use vb.net to invoke Google's Web services, that is, how to add access to Google's services in their own applications. The features described in this article include Google's search capabilities, caching capabilities, and spell-checking capabilities. The development tool we use is the vb.net in Vs.net.

Two Development steps:

The first step is to download the Developer Toolbox (Developer ' s kit) and request a license key (License key). You can go to http://www.google.com/apis/to download the developer's toolbox, which contains detailed documentation about Google's Web services, and you can develop many advanced applications by referencing the document. After downloading, you must apply for a license key, or you will not be able to access its Web service.

The second step is to create a new vb.net project. Open Vs.net, create a new project, select Visual Basic project in the project type, select Windows application in the template, and name "Googlewebservice".

Step three, add a Web reference. Select the project in Solution Explorer and select Add Web Reference in the right-click menu to insert the address URL (http://api.google.com/GoogleSearch.wsdl) of the Web Service description in the address bar of the pop-up dialog box, as shown below:



Figure 1

After adding a Web reference, we can call the various methods (method) on our computer to serve our programs, and we can also use the Vs.net development tools such as IntelliSense as we write native programs. After the Web reference has been successfully added, the corresponding Web service references are in Solution Explorer, as shown in the following illustration:


Figure 2


Also, because the project we created is a Windows application, we have to add the reference: System.Web.Services so that we can implement the development of the invocation function of the WEB service.

The fourth step is to develop the main interface of the program. You can design the main interface of the program as follows:


Figure 3

The fifth step is to write the program code. Below we write the corresponding function for each button's message.

For the first button "Start the search!" , its message response function completes the following functions: First create a Google search object, and then call the Dogooglesearch function based on the license key you entered and the content you are searching for. However, we only use a simple search function in the program, you can try to use different parameters to complete the more advanced search function. Finally, isolate the returned number of results from the search results and display them in the form of the program. The implementation of this function is as follows:

Private Sub btnSearch_Click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btnsearch.click
' Create a Google search object
Dim s as New Google.googlesearchservice ()
Try
' Invoke search function
Dim r as Google.googlesearchresult = S.dogooglesearch (txtlicensekey.text, Txtsearchterm.text, 0, 1, False, "", False, "" , "", "")
' Isolate the number of results returned from the result of the search and display it in the form of the program
Dim estresults as Integer = R.estimatedtotalresultscount
Lblsearchresults.text = CStr (estresults)
Catch ex as System.Web.Services.Protocols.SoapException
MsgBox (ex. Message)
End Try
End Sub

For the cached query! "This button's message response function, which completes the function is to get the size of the cached page. It calls the method doGetCachedPage, which allows the program to get the number of bytes cached on the page and then display the size of the cached page based on the number of bytes. The function implementation code is as follows:

Private Sub Btncache_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btncache.click
' Create a Google search object
Dim s as New Google.googlesearchservice ()
Try
' Call the doGetCachedPage method and get the number of cached bytes
Dim bytes () as System.Byte = S.dogetcachedpage (Txtlicensekey.text, Txtcachepage.text)
' Show the size of the cached page
Lblcacheresults.text = CStr (bytes. Length)
Catch ex as System.Web.Services.Protocols.SoapException
MsgBox (ex. Message)
End Try
End Sub

For the spelling checker! Button's message response function, which completes the function of checking the user's input for spelling. People who have used Google's search feature know that if you accidentally enter the error, it prompts you for the correct input, because it calls the internal spelling checker. For example, if I type "comparision of Chinese culture and Western culture" in Google, Google will prompt me to search "Comparison of Chinese culture and we Stern culture ", once it reminds me to find I have" Comparison "misspelled. So the message response function of the button calls the Dospellingsuggestion method, and if you get the appropriate spelling advice after the call, it's often a spelling mistake and appears in the form's text box control. The code for its functions is implemented as follows:

Private Sub Btnspell_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btnspell.click
' Create a Google search object
Dim s as New Google.googlesearchservice ()
Try
' Call the Dospellingsuggestion method
Dim suggestion as String = S.dospellingsuggestion (Txtlicensekey.text, Txtspell.text)
' If the right spelling suggests the information, then the display of the
If suggestion is nothing Then
Lblspellresults.text = " "
Else
Lblspellresults.text = Suggestion
End If
Catch ex as System.Web.Services.Protocols.SoapException
MsgBox (ex. Message)
End Try
End Sub


Step Sixth: Run the program. The results of the program's operation are illustrated as follows:


Figure 4


   three. Summary:

This article describes how to use vb.net to access Google's Web services, and of course you can develop such applications in other languages, such as C # or Java. By adding a reference to a Web service, a developer can develop a Web service program like a native program, so using vs.net development tools greatly simplifies the developer's work. However, this program is just a simple sample program, if you want to develop a more advanced application, you may wish to refer to the Developer Toolbox detailed documentation. Through it, I think you will be able to develop a good Google Web Services application.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.