Use C ++ builder to implement Google's Web Services)

Source: Internet
Author: User
Http://blog.csdn.net/guanwen_zhang/archive/2007/11/04/1866346.aspx

A Web Service is an additional interface provided by a website and can be called by other websites.
You can think of a Web Service as a self-contained component with one or more calling methods. He
It can be deployed anywhere in inetnet. Customers in any place in the world through the call method provided by him
. Google provides a Web service that allows you to use its database to search for webpages. Google's Web service mainly provides three methods: dogooglesearch (), dogetcachedpage (), and dospellingsuggestion (). the most common method is dogooglesearch (). The method returns results that match the query characters. Then this method returns an instance of the googlesearchresult class, which contains the search results. To implement Google's Web Services, You need to obtain a license from Google. Now this license is no longer issued, but it can be found on the Internet. The following describes how to use Google services using C ++ builder: first, create a project and save it as WebService. BPR: Save the CPP file as mainunit. CPP, and then in file | new | other... select the webservces page, select WSDL importer, and click OK. The WSDL importer wizard appears. Enter the URL of Google's Web Service Interface in location of WSDL file or URL: into _ fastcall tform1: button1click (tobject * sender)
{
Ansistring content;
Ihtmlelement * body;
Ihtmldocument * document;
Googlesearchresult * result;
_ Di_googlesearchport search;
Ansistring quer = edit1-> text; If (quer = "")
{
Showmessage ("the query content is blank! ");
Return;
}
Search = getgooglesearchport (true, "http://api.google.com/GoogleSearch.wsdl ");
Try
{
Result = search-> dogooglesearch ("tgctjkyos3yitlyzi9hg5qubry8bgqim", Quer, 0, 10, false, "", false ,"","","");
}
Catch (exception & exception)
{
Showmessage ("network fault! ");
} For (INT I = 0; I <10; I ++)
{
Ansistring Title = Result-> resultelements [I]-> title;
Ansistring url = Result-> resultelements [I]-> URL;
Ansistring text = Result-> resultelements [I]-> snippet;
Content + = "<a href =" + URL + "target = _ blank>" + title + "</a> <br>" + TEXT + "<br> <br> ";
} Cppwebbrowser1-> navigate (L "about: blank ");
Ansistring STR = "<HTML>" + content + "Sethtml (cppwebbrowser1, STR );
}
//---------------------------------------------------------------------------

The sethtml implementation functions are as follows:

Void _ fastcall tform1: sethtml (tcppwebbrowser * webbrowser, ansistring HTML)
{
Istream * stream;
Hglobal hhtmltext;
Ipersiststreaminit * PSI;

If (webbrowser-> document = NULL)
Return;
Hhtmltext = globalalloc (gptr, HTML. Length () + 1 );
If (0 = hhtmltext ){
Showmessage ("globalalloc error ");
Return;
}

Copymemory (hhtmltext, HTML. c_str (), HTML. Length ());

Olecheck (createstreamonhglobal (hhtmltext, true, & stream ));

Try {
Olecheck (webbrowser-> document-> QueryInterface (_ uuidof (ipersiststreaminit), (void **) & psi ));
Try {
Olecheck (PSI-> initnew ());
Olecheck (PSI-> load (Stream ));
} Catch (...){
Psi-> release ();
}
} Catch (...){
Stream-> release ();
}
// Delete PSI;
Psi-> release ();
// Delete stream;
Stream-> release ();
}

You also need to add the # include "mshtml. H" file to the header file, compile the connection, and generate an executable file. Add the content to be searched in edit and click button. If you need other attributes, you can select other attributes of the search result in result-> resultelements [I. The search results are finally displayed in ppwebbrowser. Due to the process of sending and parsing XML files, the display process may take some time interval and you need to wait patiently.

Related Article

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.