Historia wymaga pasterzy, nie rzeźników.


Let's first create a VB6 standard application. We need to add a reference to Microsoft XML, v3.0
( msxml3.dll ), because we'll use the XMLHTTP object to help us communicate with the web services.
For demonstrative purposes, we will also use the Microsoft Internet Controls component ( shdocvw.dll ) to display XML and HTML content.
First, add two buttons on the default form, form1 , and give them the captions GET and POST , as well as the names cmdGet and cmdPost, respectively. After that, drag the WebBrowser object from the toolbar onto the form, and name the control myWebBrowser. If you make the WebBrowser navigate to about:blank initially, you will end up with something like Figure 6 -5 .
Figure 6 -5. VB client form to test Web Services

Now all we need is some code s imilar to the following to handle the two buttons' click events:

Private Sub cmdGet_Click( )
Dim oXMLHTTP As XMLHTTP
Dim oDOM As DOMDocument
Dim oXSL As DOMDocument

' Call the web service to get an XML document
Set oXMLHTTP = New XMLHTTP
oXMLHTTP.open "GET",_
"http://localhost/PubsWS/PubsWS.asmx/GetBooks", _
False
oXMLHTTP.send
Set oDOM = oXMLHTTP.responseXML

' Create the XSL document to be used for transformation

140
.NET Framework Essentials
Set oXSL = New DOMDocument
oXSL.Load App.Path & "\templateTitle.xsl"

' Transform the XML document into an HTML document and display
myWebBrowser.Document.Write CStr(oDOM.transformNode(oXSL))
myWebBrowser.Document.Close

Set oXSL = Nothing
Set oDOM = Nothing
Set o XMLHTTP = Nothing
End Sub

Private Sub cmdPost_Click( )
Dim oXMLHTTP As XMLHTTP
Dim oDOM As DOMDocument
Dim oXSL As DOMDocument

' Call the web service to get an XML document
Set oXMLHTTP = New XMLHTTP
oXMLHTTP.open "POST", _
"http://localhost/PubsWS/PubsWS.asmx/GetAuthor", _
False
oXMLHTTP.setRequestHeader "Content-Type", _
"application/x-www-form-urlencoded"
oXMLHTTP.send " sSSN=172 -32-1176"
Set oDOM = oXMLHTTP.responseXML

' Create the XSL document to be used for transformation
Set oXSL = New DOMDocument
oXSL.Load App.Path & "\templateAuthor.xsl"

' Transform the XML document into an HTML document and display
myWebBrowser.Document.Write oDOM.transformNode(oX SL)
myWebBrowser.Document.Close

Set oXSL = Nothing
Set oDOM = Nothing
Set oXMLHTTP = Nothing
End Sub
The two subroutines are similar in structure, except that the first one uses the HTTP GET protocol and the second one uses the HTTP POST protocol to get to the PubsWS web service. Let's take a closer look at what the two subroutines do.
For the HTTP GET protocol, we use the XMLHTTP object to point to the URL for the web method, as specified in the WSDL. Since the GetBooks web method does not require any parameters, the query string in this case is empty. The method is invoked synchronously because the async parameter to XMLHTTP's open method is set to false . After the method invocation is done, we transform the XML
result using templateTitle.xsl and display the HTML on the myWebBrowser instance on the form.
Figure 6-6 displays the screen of our web services testing application after invoking the GetBooks web method at URL http://localhost/PubsWS/PubsWS.asmx/ through HTTP GET protocol.
Figure 6 -6. VB client form after calling GetBooks

141

For the HTTP POST protocol, we also point the XMLHTTP object to the URL for the web method—i n this case, method GetAuthor. Because this is a POST request, we have to specify in the HTTP header that the request is coming over as a form by setting the Content -Type header variable to application/x-www-form-urlencoded. If this variable is not set, XMLHTTP by default passes the data to the server in XML format.
Another difference worth noticing is that the GetAuthor method requires a single parameter, which is the SSN of the author as a string. Since this is a post request, we are going to send the name/value pair directly to the server in the body of the message. Because the Content-Type header has been set to application/x-www-form-urlencoded, the server will know how to get to the pa rameters and perform the work requested. This time, we use templateAuthor.xsl to transform the XML result to HTML and display it. Figure 6 -7 shows our application after invoking the GetAuthor web method of PubsWS web service through HTTP POST protocol.
Figure 6-7. VB client form after calling GetAuthor

The following code is the XSL used to transform the XML result from the GetBooks web method call to HTML to be displayed on the web browser instance on the VB form:
<html version="1.0" xmlns:xsl="http://www.w3.org/TR/WD -xsl">
<head><title>A list of books</title></he ad>
<style>
.hdr { background-color=#ffeedd; font-weight=bold; }
</style>
<body>
<B>List of books</B>
<table style="border -collapse:collapse" border="1">
<tr>
<td class="hdr">Title</td>
<td class="hdr">Type</td>
<td class="hdr">Price</td>

142
.NET Framework Essentials
<td class ="hdr">Notes</td>
</tr>
<xsl:for-each select="//Books">
<tr>
<td> <xsl:value-of select="title"/> </td>
<td> <xsl:value-of select="type"/> </td>
<td> <xsl:value-of select="price"/> </td>
<td> <xsl:value-of select="notes"/> </td>
</tr>
</xsl:for -each>
</table>
</body>
</html>
Here is the XSL used to transform the XML result from the GetAuthor web method call to HTML to be displayed on the web browser instance on the VB form:
<html version="1.0" xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<head><title>Selected author</title></head>
<STYLE>
.hdr { background -color:'#ffeedd';
text-align:'right'; vertical -align:'top';
font-weight=bold; }
</STYLE>
<body>
<B>Selected author</B>
<xsl:for-each select="//SelectedAuthor">
<table style="border-collapse:'collap se'" border="1">
<tr><td class="hdr">ID</td>
<td ><xsl:value-of select="au_id"/> </td></tr>
<tr><td class="hdr">Name</td>
<td> <xsl:value-of select="au_fname"/>
<xsl:value-of select="au_lname"/> </td></tr>
<tr><td class="hdr">Address</td>
<td> <xsl:value-of select="address"/> <br>

Podstrony