asp如何制作发布webservice-III
In Part 2 we took an indepth look at the .WSDL
and .wsml
files created by the SOAP Toolkit when creating a Web service. Now that we have a Web service, let's look at how to create a component to consume a Web service, and how to use this component through an ASP page!
Building a Component to Consume a Web Service In order to consume a Web service, you must build a COM component that uses the Microsoft Soap Type Library. To accomplish this, create COM component through VB6 and, from Projects/References, include the Microsoft Soap Type Library. Now, for our client component, use the project and class named prjAddClient
and AddClient
, respectively.
Ideally, our component that consumes the Web service will have a method for each method exposed in the Web service. Additionally, each of these methods should have the same input and output parameters. So, for this example, we'll create a single function, Add
, which has the same definition as the Web services Add
method:
Public Function Add(x As Double, y As Double) As Double
The code for this function is quite a bit different than the code for our Web service's Add
method. This Add
method must call the Web service's Add
method, passing in the values of x
and y
and returning the result of the Web service's Add
method. To call the Web service's Add
method, we use the following code:
Public Function Add(x As Double, y As Double) As Double Dim objSoapClient As New SoapClient objSoapClient.ClientProperty("ServerHTTPRequest") = True Call objSoapClient.mssoapinit("http://localhost/AddServer.wsdl", _ "AddServer", _ "AddServerSoapPort") Add = objSoapClient.Add(x, y) End Function
Note that in our call to mssoapinit
we pass in the location of the .WSDL
file that describes the Web service. You will need to change the URL to the location of the .WSDL
file of the Web service that you wish to access. We then call the Add
method of the Web service passing in the values of x
and y
(objSoapClient.Add(x, y)
). This result is returned as the return-value of the Add
method.
Calling the Web Service-Consuming Component through an ASP Page In theory calling the component that consumes the Web service from an ASP page should be an easy task. Since the component that creates the Web service is a local component, just use Server.CreateObject
to create an instance of the component in an ASP page and call the Add
method. In fact, the following code should do the trick:
'Create an instance of the component Dim objAdd Set objAdd = Server.CreateObject("prjAddClient.AddClient") 'Add two numbers and output the value! Response.Write(objAdd.Add(4,5))
However, when you first try it, you'll probably get a cryptic error that reads something like: WSDLReader:Loading of the WSDL file failed
. Microsoft explains that "due to limitations of Microsoft Win32 Internet (WinInet) functions and the XMLHTTP object, the ServerXMLHTTP object was created to allow you to establish server-to-server HTTP connections." ServerXMLHTTP is what is used by the component created to consume the Web service. Microsoft points out that "the ServerXMLHTTP object does not rely on WinInet but rather on a new HTTP client stack, which is a subset of WinInet and offers a 'server-safe' connection.'" (Read the Microsoft KB article these quotes were taken from.) This can cause problems when using ServerXMLHTTP through IIS.
You have two options: set the IIS Application Protection setting to Low (not recommended) or download Microsoft's free Proxy tool. Once this tool is downloaded, you will need to run it from the command-line passing in certain parameters depending on your proxy settings. (If you don't use a proxy, specify proxycfg -d
.) Once you've configured the proxy, you need to stop and restart IIS. (For more inforamtion on using Proxycfg.exe
be sure to check out this KB article.)
Note that, once you get this working and are successfully loading your ASP page, you are using a Web service! Neat, eh? You ASP Web page is (indirectly) invoking a component on another Web server which is running and returning its results to your ASP page. Welcome to distributed computing.
Conclusion In this article we examined how to create (and consume) a Web service using Visual Basic 6 and an ASP page. Note that to accomplish all of this we did have to jump through some hoops, such as downloading the SOAP Toolkit. Fortunately, ASP.NET makes Web service development and consumption a much simpler (and quicker) process. For more information on creating and consuming Web services with ASP.NET, be sure to read:
- Using ASP.NET/WebServices For UPS Shipping Quotes Creating a .NET Web Service
- Building Client Interfaces for .NET Web Services
- Introducing Web Services in ASP.NET
Happy Programming!
加支付宝好友偷能量挖...