Using a Web Service

Developers can usually choose how they consume a Web Service. They can invoke the service by SOAP, GET, or POST methods. As we saw earlier, the service's WSDL contract establishes the valid communications protocols. Services generally support all three methods, however. Of the three, SOAP is the richest in terms of support for complex arguments and return results.

SOAP requests can be built and sent manually by the application developer. This is a painstaking process because all the SOAP infrastructure XML elements must be written by hand. (For a more detailed discussion on SOAP documents, see Chapter 14, "More About SOAP"). Furthermore, the programmer is responsible for posting the document to the service and unpacking the XML result. This methodology gives the developer the finest level of control. The service result document can be parsed into a proprietary data structure or forwarded to another destination.

Such low-level access to the messages, however, is tedious, error-prone, and rarely necessary. If you are planning to build an application that utilizes Web Services, you might want to look into a developer tool suite to help you with some of the plumbing. Nevertheless, examining the low-level calling syntaxes to see how it's done can be useful.

HelloWorld via SOAP

The following is an example of a SOAP request to the HelloWorld service. The first section is the HTTP request header that must be sent to the Web server. The second section consists of the SOAP envelope. The SOAP body contains the payload of the request that states the service to be called (HelloWorld) and supplies any required arguments (<name>John</name>).

 POST /HelloWorld/HelloWorld.asmx HTTP/1.1 Host: localhost Content-Type: text/xml; charset=utf-8 Content-Length: <length> SOAPAction: "http://tempuri.org/HelloWorld" <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <HelloWorld xmlns="http://tempuri.org/"> <name>John</name> </HelloWorld> </soap:Body> </soap:Envelope>

The SOAP response from the Web Service includes a similar HTTP header and SOAP envelope. This time, however, the body contains the results from the call to the service. You might notice that the request and the response are both consistent with the semantics stated in the WSDL contract document. In this case the result is nested inside a <HelloWorldResult> tag. Obviously, the syntax of the request and response documents is service-specific.

 HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: <length> <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <HelloWorldResponse xmlns="http://tempuri.org/"> <HelloWorldResult>Hello, John</HelloWorldResult> </HelloWorldResponse> </soap:Body> </soap:Envelope>

Microsoft was the primary sponsor of the SOAP specification. They released version 0.9 of the specification in 1999. The W3C released version 1.1 in May 2000. Both parties kept the protocol simple, which is one of the reasons why it was so successful. SOAP version 1.2 is under active development at W3C. For more information, see http://www.w3.org/TR/soap12/.

HelloWorld via HTTP GET

The HTTP GET method is the simplest way to invoke a Web Service. All of the argument data to the service is passed inside the URL string in URLEncoded format. This request string shows how it's done for HelloWorld:

 GET /HelloWorld/HelloWorld.asmx/HelloWorld?name=John HTTP/1.1 Host: localhost 

The Web Service cannot return a SOAP document because the request was not made using SOAP. Therefore, the service must return a simple XML document in the HTTP response with the result of the function call.

 HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <string xmlns="http://tempuri.org/">Hello, John</string> 

HelloWorld via HTTP POST

HTTP POST differs from HTTP GET for Web Service invocations the same way it differs in HTML Form posts. All the arguments to the service are stored in the HTTP message body as name-value pairs instead of inside the URL string. This approach is more secure and robust than HTTP GET but not as flexible as using SOAP. The HTTP POST request to the HelloWorld service follows.

 POST /HelloWorld/HelloWorld.asmx/HelloWorld HTTP/1.1 Host: localhost Content-Type: application/x-www-form-urlencoded Content-Length: <length> name=John 

The HTTP POST response document is identical to the HTTP GET response document. The response is formatted in plain XML without any SOAP references.

 HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <string xmlns="http://tempuri.org/">John</string> 


XML Programming
XML Programming Bible
ISBN: 0764538292
EAN: 2147483647
Year: 2002
Pages: 134

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net