Transform COM components to Web Services

Source: Internet
Author: User
Tags soap client

Microsoft's. NET development platform has been released for a long time. Do we need to discard the previously written components and transfer them to VB. NET and C? The answer is no. We do not need to discard the previous components, but can use them. Most program developers have to pay a certain price to migrate from the previous development environment to VB. NET and C. However, to maintain the compatibility of existing programs, you still need to use VB6. Fortunately, Microsoft's soap development tool Simple Object Access Protocol (SOAP) toolkit gives us an ActiveX dll that can be compiled using VB6, we can also use the benefits brought by Web Services, which allows our components to be used on the Web as a service through soap.

Soap makes it easy for us to write distributed applications. We can use it to call components in a platform-independent and location-independent way. The soap client sends the request information to the server, which requires the execution of the component function. The server processes the sent request, executes the function we need, and then sends the execution result to the client in response. These messages are in XML format and can be transmitted between clients and servers using multiple protocols. We use the most common HTTP protocol as an example.

Soap uses HTTP as a transmission mechanism to allow the applications we create to be local, lan, or Internet. Because XML is platform-independent, applications we write can be applied on Windows, UNIX, and handheld or wireless devices.

Now let's take a look at how to convert our ActiveX DLL to Web services through the soap toolkit. Surveys are the most common content on the Internet. We use surveys as a component that can be used by other applications and web pages. The testservey component of surveys implements three functions: requestsurvey, respondtosurvey, and surveyresults.

Open Visual Basic6.0 and create an ActiveX dll project named testsurvey. VB6 automatically adds a class named class1 and changes it to survey. The project properties are shown in:

Enter the following code:

Option explicit

Public Function requestsurvey (intsurveyid as integer) as string

Dim strsurvey as string

'Is just a simple example

Strsurvey = "do you think Web services can have a revolutionary impact on it? "

Requestsurvey = strsurvey

End Function

Public sub respondtosurvey (intsurveyid as integer, blnagree as Boolean)

'The code to save the investigation to the database is written here. To save space, the code is omitted here as an example.

End sub

Public Function displaysurveyresults (intsurveyid as integer) as string

Dim strsurveyresults as string

'Is just a simple example

Strsurveyresults = "A total of 20 people voted, 16 people agreed, and 4 people did not agree. "

Displaysurveyresults = strsurveyresults

End Function

The requestsurvey function returns the problem of investigation based on the ID number of the investigation. The respondtosurvey function saves the investigation result to the database. The displaysurveyresults function is used to return the survey results. Finally, compile and generate testsurvey. dll.

Next, open the soap toolkit. If it is not installed, you can go to the producer. Run "Soap Toolkit's Web Services Description Language (WSDL) generator" from the "Start" menu: Start | program | MICROSOFT soap Toolkit version 3 | WSDL generator, then follow the wizard. First, a welcome window appears, as shown in the following figure:

Click "Next". The following page is displayed:

You can select a configuration file that allows us to reload or change the settings of the component we previously generated using the generator, this is useful when adding new methods for components or removing components from different servers. Here we do not select any one. Click "Next" and the following window appears. Enter the Web Services Name and DLL file location:

We enter the Web Services Name testsurvey, select the location of our compiled testservey. dll, and click "Next". The following window appears:

We are required to select the class and method we want to use as Web services. We should select all methods of the testsurvey class. Click "Next". The following window is displayed, asking us to select the server address to be listened to and the method of listening for requests from the soap client.

Click "Next". The following window appears, allowing you to enter some URL addresses used by the WSDL:

Click "Next". The Wizard will allow you to enter the path for storing the new file and the path for storing the configuration file, as shown below:

Click "Next" and click "finish. The soap generator automatically generates four files to save all the values you entered earlier: testsurvey. WSDL, testsurvey. wsml, testsurveyclient. wsml, testsurvey. asp, and a configuration file testsurvey. wgen.

Next, we need to test the results created by the wizard, testsurvey. the ASP file contains the method to call Web Services, testsurvey. the WSDL file is an XML definition for shells outside the LAN. To use your web services, a client that can use soap must understand the methods and parameters that can be used. The testsurvey. WSDL file describes how to use your web services to define requests and responses in XML format. Soap toolkit3.0 generates two wsml files, testsurvey. wsml identifies the progid of testservey and also contains the ing between the request information in the WSDL file and the COM interface, testsurveyclient. wsml contains any complex data type ing. If you define a complex data type, this file will be used.

The WSDL file splits all your methods into individual request and response information elements. In soap, the client sends a request to Web Services, which provides the name attribute, contains the class name and method name. This file also contains survey. the requestsurvey request information element, which is automatically generated by the soap generator by including the class name and method name in the DLL. When it receives the request information, the server executes the web services function and returns the response. The name element is basically the same as the request information. The soap generator automatically adds the "response" suffix. For example, the response to the requestsurvey method will correspond to survey. requestsurveyresponse.

The soap generator assigns each parameter to an XML part element and matches the name attribute with the parameters in our DLL function, the generator also translates parameter types into equal Data Types in soap. When creating a WSDL file, the builder simply maps data types. If some parameters in the ing method cannot be used, seven question marks (?) are used. . The ing between the soap data object and the VB6 data object:

Next, we will create a virtual directory so that the file we just created can be used by the Internet Information Server (IIS) Web server. Open the Internet Service Manager and use the creation Wizard to create a virtual directory. the alias is the same as what we specified in wdsl. The physical directory points to the directory specified when we saved the new file. After the virtual directory is created, we can test our web services.

Now, we create a simple soap client to use our web services. Open visual basic6 and create a "Standard EXE" project:

Option explicit

Private sub form_load ()

Dim soapclient as object

'Create a soap client instance

Set soapclient = Createobject ("mssoap. soapclient30 ")

'Initialize the soap Client

Call soapclient. mssoapinit ("http: // localhost/soaplisten/testsurvey. WSDL", "testsurvey ")

'Now that the soapclient object references our web services, we can call our method. We take the investigation id = 1 as an example:

Lblsurveyquestion. Caption = soapclient. requestsurvey (1)

Set soapclient = nothing

End sub

When the user clicks the "Vote" button, the result is sent to the investigation id = 1 and the investigation result is returned to the client. First, create a soap client instance:

Private sub cmdsubmit_click ()

Dim soapclient as object

Dim strsurveyresults as string

Set soapclient = Createobject ("mssoap. soapclient30 ")

Call soapclient. mssoapinit ("http: // localhost/soaplisten/testsurvey. WSDL", "testsurvey ")

'Send the survey and return the result

Call soapclient. respondtosurvey (1, optagree. value)

Strsurveyresults = soapclient. displaysurveyresults (1)

Call msgbox (strsurveyresults, vbinformation + vbokonly, "survey responses ")

End sub

Add the label control named lblsurveyquestion in the survey form to display the survey problem. Two radio buttons named optagree (title: "agree ") and optdisagree (title: "disagree"), a command button named cmdsubmit. In the form load event, we return the problem request and send it to Web Services with the submit button, and then return the result. The test result is as follows:

From the above example, it can be seen that accessing Web services is very simple. Calling Web Services in VB is just like directly calling the mssoap. soapclient30 method. Is it very convenient.

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.