Develop a simple multi-threaded Web Service client with Borland C ++ Builder 6.0

zhaozj2021-02-16  41

Borland's latest C Builder 6.0 (hereinafter referred to as BCB) and Delphi / Kylix, there is a powerful web service processing power, but now there is not much information on how to create and call Web service with BCB, now I demonstrate How to use BCB6.0 to implement a simple web service client, and use in my example is a multi-threaded call mode. Since the time you use BCB is very short, the development experience is not enough, so this article is only a throwing jade. If you have errors in the text, please also ask you more.

Now let's start. We want to call a famous DR.BOB written by Delphi6, a Web Service for Delphi, CPPBuilder, Kylix, JBuilder, etc. WSDL is http://www.ebob42.com/cgi-bin/ DRBOBSCLINIC.EXE / WSDL / IHEADLINE. You can see it and more about Web Service on www.xmethods.com.

1. First create a new project: file-new-application, put on the MEMO, ComboBox, and Button controls on Form1, and the interface effect is as shown below. Then save the unit and project with the default name (you can also give a meaningful name to the unit and projects).

2. Set some properties of ComboBox1: We want to call different product information, which is determined by the value in ComboBox1. Open its items property editor, enter the following:

Delphi

C Builder

JBuilder

Kylix

SOAP

Borcon

Then, set the itemIndex attribute of ComboBox1 to 0, so "delphi" will become default.

3, now you have to import the Web Service interface through WSDL Importer. Keep the existing project constant, then select File-New-Other, in the Web Service page, click the WSDL Importer item to determine. In the wizard that appears, enter: http://www.ebob42.com/cgi-bin/drbob42.com/cgi-bin/drbobsclinic.exe/wsdl/iheadline, click Next. WSDL Importer will automatically obtain the relevant interface information, and the result is as shown in the following figure:

Click Finish to exit. Save the unit under the project directory with the default name IHEADLINE.CPP.

4, now, we have interface information, start calling it. I have said in front, I want to call with multi-thread. Why is this? Before this, I have been in Delphi for web service call. At that time, the information (including Li Wei teacher wrote) is a single thread, but I found the effect in the process of use. Because Web Service is very resource, plus XML package, conversion, service and client data processing, often a simple call will take time, and the client interface will be lost as a deadline like death. Based on the above consideration, I want to solve the problem by establishing another thread, call and data processing in the background through the new thread, and finally reacting the result to the VCL interface of the main thread. This is my consideration, of course, there may be an immature place.

5, next is to create a new thread. Also keep the existing project unchanged, click File-New-Other, and click the Thread Object item to determine in the New page. Enter the class and name of the thread in the subsequent dialog. The effect on my machine is this: After confirming, save the unit to the same directory with getnews.cpp.

6, now to specifically write code for new threads. First of all, we must use the interface information that is automatically imported in this thread, but also exchange information with the main thread, so click File-Include Unit HDR ..., in the subsequent dialog box, put the Unit and GetNews two headers are selected, otherwise it will be reported when compiling.

Then, declare such a function in the getNews.h header:

Void __fastcall threadgetnews ();

You can declare it as a private function of the GetNews class. Then implement this function in the getnews.cpp file, the code is as follows:

/ / -------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------

Void __fastcall tgetnews :: threadgetNews ()

{

FORM1-> Button1-> enabled = false; // The purpose is to make only one instance during the new thread operation.

Ansistring s;

Switch (Form1-> ComboBoX1 -> ItemIndex) / * Decide which function to call through different items in ComboBox * /

{

Case 0: // Delphi

s = getiheadline () -> Delphinews (0);

Break;

Case 1: // CPPBuilder

s = getiheadline () -> cbuildernews (0);

Break;

Case 2: // jbuilder

s = getiheadline () -> jbuildernews (0);

Break;

Case 3: // kylix

s = getiheadline () -> KylixNews (0);

Break;

Case 4: // SOAP

s = getiheadline () -> soapnews (0);

Break;

Case 5: // borcon

S = GetiheadLine () -> borconnews (0);

Break;

DEFAULT:

S = "invalide direction";

}

FORM1-> Memo1-> Lines -> Clear ();

Form1-> Memo1-> lines-> add (s);

FORM1-> Button1 -> enabled = true; / * End (unlikely said), let Button1 can be used again * /

s = "";

}

/ / -------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------

Note: If you have forgotten to quote both headers, then this step is confirmed.

7, then set how the thread starts. First, please find the definition of __fastcall tgetnews :: tgetnews: tthread (createSuspended) in getnews.cpp, then add:

PRIORITY = TPNORMAL;

Set threads run in normal priority. You can also set additional priority.

Then, add the code in the execute () function, making it looks below: // ----------------------------- -----------------------------------------

Void __fastcall tgetnews :: execute ()

{

SetName ();

// ---- Place Thread Code Here ----

// The following code is new

FreeOnterminate = true;

IF (! terminated)

Synchronize (ThreadgetNews);

}

/ / -------------------------------------------------------------------------------------------- -----------------------

In this way, the settings of the new thread are completed. It can be seen that one of the new threads is created in the main thread, and the new thread will execute the code in Execute () if the result () is not ended, and then the threadgetNews will be called via Synchronize. By this function to call the web service and get the result, implement the update of the main thread VCL interface. First, the thread call is complete, it will automatically recycle resources. (Because we set freeOnterminate to True).

8. Now, we can call this new thread in the main thread. Switch to Form1, first join the reference to the threaded header file: #include "getnews.h" (or add the above method to join the File menu), then enter the following code in the Button1 on /click event:

/ / -------------------------------------------------------------------------------------------- -----------------------

Void __fastcall tform1 :: button1click (Tobject * Sender)

{

TgetNews * gnews = new tgetnews (false);

}

/ / -------------------------------------------------------------------------------------------- ----------------------

OK! Datun is about to be gone. You can compile items now. After running, choose the theme of your favorite in ComboBox, then press Button, there will be information to pass in MEMO. This is this on my machine:

Of course, here is the ASCII character, in this web service, you can also choose to return to HTML format, or even WMLs on the PDA. You can set a function such as Delphinews (int), the parameter values ​​of the above three formats are 0, 1, 2, respectively. For convenience, I set to 0 in this example.

However, there seems to be a problem, that is, when running this program, the new thread performs web service call in the background, but the main interface is still a bit stagnant, this is a bit different from the ordinary multi-threaded program, as for everyone, please Solve it. My Mail is: Hada@shcei.com.cn.

转载请注明原文地址:https://www.9cbs.com/read-27557.html

New Post(0)