Lab content
Learn how to call existing Web Services.
Write programs and call
No more nonsense about Web Services. Let's talk about how to use it.
1. You need the Java environment to http:. // java.sun.com/j2se. You can download Java Development Kit
2. The Google api jar file is required. You can download it at http://www.google.com/apis. Add classpath.
3. Apply for a Google access account. Currently, Google supports free application. You need to use an email account to obtain a new account. Note: currently, the free account supports a maximum of 1000 queries per day. Application address: http://www.google.com/apis.
Enable your account in your mailbox after applying, and then send another email to your mailbox. Obtain your license key.
The format of the SOAP request is not mentioned. Google directly provides the API. Do not waste googleapi. jar.
4. The following is an example
Package daxigua;
Import com. Google. Soap. Search .*;
Import java. Io .*;
Public class googleapitest {
Public static void main (string [] ARGs ){
String clientkey = "clientkey"; // The obtained license key, not the account name.
String query = "csdn ";
Googlesearch S = new googlesearch ();
S. setkey (clientkey );
Try {
S. setquerystring (query );
S. setlanguagerestricts ("lang_cn ");
Googlesearchresult r = S. dosearch ();
System. Out. println ("Google search results:" + R. tostring ());
Filewriter fw = new filewriter ("result.txt ");
FW. Write (R. tostring ());
FW. Close ();
} Catch (googlesearchfault f ){
System. Out. println ("the call to the Google Web APIs failed:" +
F. tostring ());
} Catch (exception e ){
E. printstacktrace ();
}
}
}
For specific use, refer to the doc api.zip doc directory. There are 6 classes in total.
The Web Service released by Google allows the program to send queries and accept and print the results of Google queries.