Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
3Activity
0 of .
Results for:
No results containing your search query
P. 1
SOAP

SOAP

Ratings:

4.0

(1)
|Views: 644|Likes:
Published by sony

More info:

Published by: sony on May 28, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

08/09/2012

pdf

text

original

 
http://www.developerfusion.co.uk/show/24/3/
Introduction
SOAP – Simple Object Access Protocol – is the hottest thing in XML development right now. Ithas a major role to play in Microsoft’s next generation of Visual Studio, and it is the basis of their “.NET” strategy. If you’re looking to write a bullet-proof SOAP service now, in VB6, thenlook no further than Microsoft’s SOAP toolkit for VB; but if you really want to
understand
what SOAP is all about, then you need to get under the hood, and start building your ownobjects around the SOAP standard. This article aims to get you started.In the article, we will create a simple SOAP service, and a client to access that service. Theservice will be in the form of an Active Server Page, which I have called
soap.asp
- it willneed to live in the root directory of your Personal Web Service (by default
c:\inetpub\wwwroot
) Our service will accept and process SOAP requests made by ourclient – a VB exe, with a single module, and a start-up
Sub Main()
Step by Step
As we know, SOAP is a call-response mechanism, which operates in a client-server paradigm.The client (your application) makes a
call 
to the server (a web server, somewhere on theinternet), passing in parameters; and the server provides a
response
. Both call and responseare transported in the form of XML documents. Therefore, to build our own example SOAPsystem, we need both a client and a server – a
caller 
and a
responder 
.The example we will follow is a very simple one: we will build a service that will calculate thetax due on a sales transaction. In traditional VB terms well will create a function with thefollowing definition:
Public Function GetSalesTax(ByVal pSalesTotal As Double) as Double
 
GetSalesTax = pSalesTotal * 0.04
 
End Function
 Crude, but effective (if you live in a state where the sales tax rate is 4%!)This function defines a method name (GetSalesTax), a parameter (pSalesTotal – representingthe total value of the sale) and a return value (the result of the function). In more traditionalobject-oriented terms, we might think of pSalesTotal as an “IN” parameter, and theGetSalesTax result as an “OUT” parameter. Quite straightforwardly: sales-total in; sales-taxout. A SOAP service can be thought of in terms of “IN” and “OUT” parameters – the clientpasses “IN” parameters to the server, and then receives the “OUT” parameters in return. So,in order to create our SOAP service, we need a server that will listen for requests toGetSalesTax, accompanied by “IN” parameters, and which will respond with “OUT” parameters, indicating the correct tax amount. Let’s start with the client – the
caller 
. How dowe call the SOAP service to make our request.
The Client
 
In traditional VB terms, our request to the GetSalesTax function described above, would besomething such as:
dblSalesTax = GetSalesTax(100)
 Returning a value of $4.If the GetSalesTax function was contained within an external object, such as an MTS server,then the call would need to reference the server DLL:
Dim objTax As New CTaxCalcdblSalesTax = objTax.GetSalesTax(100)
 In a SOAP system the call is little different, only the request is formatted as an XML document,which is passed up to the server. The appropriate document contains the same details as theMTS call –a method to call, and the parameter name and value.
<GetSalesTax><SalesTotal>100</SalesTotal><GetSalesTax>
 In order to ensure that the server can correctly identify and decrypt this method call, it iswrapped up in a larger document, called a SOAP envelope, which references the universalname-space of the SOAP envelope standard.
<SOAP:Envelope xmlns:SOAP="urn:schemas-xmlsoap-org:soap.v1"><SOAP:Header></SOAP:Header><SOAP:Body><GetSalesTax><SalesTotal>100</SalesTotal><GetSalesTax></SOAP:Body></SOAP:Envelope>
 Finally, to complete our SOAP request document, we will add a name-space reference to ourmethod call, which points to the object which contains the method – the equivalent of theobject declaration (
Dim objTax As New CTaxCalc 
) in our MTS method call.
<SOAP:Envelope xmlns:SOAP="urn:schemas-xmlsoap-org:soap.v1"><SOAP:Header></SOAP:Header><SOAP:Body><m:GetSalesTax xmlns:m="urn:myserver/soap:TaxCalc"><SalesTotal>100</SalesTotal></m:GetSalesTax></SOAP:Body></SOAP:Envelope>
 Now that we have built our SOAP request document, we are ready to send it to the server. Therequest is a simple HTTP post - just like posting a web form in your internet browser. Of course, your internet browser masks all the complexity of sending a form to a server; and inthe longer-term .NET will mask this process from your VB code too. But for now, we need todo the job ourselves; and I have been using Microsoft's XML HTTP Request object to give me ahelping hand. (The XMLHTTPRequest is an object within the MSXML class library (MSXML.DLL),
 
and it comes with IE5.) Assuming that strEnvelope contains the XML document describedabove, the request is formatted thus:
VB
Dim objHTTP As New MSXML.XMLHTTPRequestDim strEnvelope As String'Set up to post to our localhost serverobjHTTP.open "post", "http://localhost/soap/soap.asp"'Set a standard SOAP/ XML header for the content-typeobjHTTP.setRequestHeader "Content-Type", "text/xml"'Set a header for the method to be calledobjHTTP.setRequestHeader "SOAPMethodName", _"urn:myserver/soap:TaxCalc#GetSalesTax"'Make the SOAP callobjHTTP.send strEnvelope'Get the return valuestrReturn = objHTTP.responseBody
 
C#
MSXML.XMLHTTPRequest objHTTP= newMSXML.XMLHTTPRequest(); stringstrEnvelope;// Set up to post to our localhost serverobjHTTP.open;"post";,"http://localhost/soap/soap.asp";objHTTP.setRequestHeader;"Content-Type";,"text/xml";objHTTP.setRequestHeader;"SOAPMethodName";,"urn:myserver/soap:TaxCalc#GetSalesTax";objHTTP.send;strEnvelope;// Get the return valuestrReturn=objHTTP.responseBody;
Now that we have sent our request, we move to the server, to see how we set up a SOAPservice to listen and respond to our calls.« Step by Step|The Server »
The Server
Our server needs to be configured to accept the HTTP post sent by the client. It will be notedthat we direct the client’s post to a URL on our local server – http://localhost/soap.asp. So ourfirst job is to create this page, to listen for, and process, SOAP calls to our server. We knowthat our ASP will receive an XML document, in the form of an HTTP post, with a method name(GetSalesTax), and a parameter (SalesTotal). So, to write our basic listener service, all we

Activity (3)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Nana Yaa Aprekua liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->