Client use WSDL Generated Proxy Object
If your client will be Windows applications or ASP.NET applications, you can use WSDL.EXE utility to created standard .NET Assemble to provide Proxy Class for your clients.
Here are steps you can follow and try:
Use WSDL.EXE utility to create the Proxy Class source file in any language you have chosen and here I use C# and command as follows:
Hide Copy Code
C:\>wsdl /language:C# /out:MyProxyClass.cs http://localhost/ASP.NET/MyWebService.asmx
MyProxyClass.cs is generated and source listing as follows:
File: MyProxyClass.cs
Hide Shrink
Copy Code
//------------------------------------------------------------------------------
// <autogenerated>
// This code was generated by a tool.
// Runtime Version: 1.0.2914.16
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------
//
// This source code was auto-generated by wsdl, Version=1.0.2914.16.
//
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.Web.Services;
[System.Web.Services.WebServiceBindingAttribute(Name="MyClassSoap", Namespace="http://tempuri.org/")]
public class MyClass : System.Web.Services.Protocols.SoapHttpClientProtocol {
[System.Diagnostics.DebuggerStepThroughAttribute()]
public MyClass() {
this.Url = "http://localhost/ASP.NET/MyWebService.asmx";
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/Add",
Use=System.Web.Services.Description.SoapBindingUse.Literal,
ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public int Add(int a, int b) {
object[] results = this.Invoke("Add", new object[] {
a,
b});
return ((int)(results[0]));
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
public System.IAsyncResult BeginAdd(int a, int b, System.AsyncCallback callback, object asyncState) {
return this.BeginInvoke("Add", new object[] {
a,
b}, callback, asyncState);
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
public int EndAdd(System.IAsyncResult asyncResult) {
object[] results = this.EndInvoke(asyncResult);
return ((int)(results[0]));
}
}
Then we need to create the .NET
Assembly for used by clients:
Hide Copy Code
C:\> csc /t:library MyProxyClass.cs
The above command will compile the source and create MyProxyClass.dll library file.
I use ASP to depict how to use the proxy object and the file is TestWebServiceWithProxy.aspx source listing as follows:
File: TestWebServiceWithProxy.aspx
Hide Copy Code
<%@ page language="C#" %>
<html>
<script runat="server">
void btn_click(Object source, EventArgs e)
{
MyClass mycls = new MyClass() ;
int x = Int32.Parse(a.Text) ;
int y = Int32.Parse(b.Text);
Message.Text = mycls.Add( x, y).ToString() ;
}
</script>
<body>
<form Action = "TestWebServiceWithProxy.aspx" runat="server">
<asp:TextBox id="a" runat="server" />
<asp:TextBox id="b" runat="server" />
<asp:button id=btn OnClick="btn_click" Text="Enter" runat="server" />
<p><asp:label id="Message" runat="server" /></P>
</form>
</body>
</html>
Client use XMLHTTP to call Web service via SOAP
To fully explore the SOAP capability, you may choose to call your ASP.NET Web Service via SOAP core protocol and here I provide another example for reference.
To test the ASP.NET service with SOAP protocol, I create an ASP client file TestWebServiceByXML.asp and its source is listed as follows:
File: TestWebServiceByXML.asp
Hide Shrink
Copy Code
<html>
<body>
<script language="jscript">
function btn_click (a, b)
{
var xmlObj = new ActiveXObject("Msxml2.DOMDocument") ;
var sXml = "<?xml version=\"1.0\" ?>" ;
sXml += "<soap:Envelope "
sXml += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " ;
sXml += "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " ;
sXml += "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" ;
sXml += "<soap:Body>" ;
sXml += "<Add xmlns=\"http://tempuri.org/\">" ;
sXml = sXml + "<a>" + a.value + "</a>" ;
sXml = sXml + "<b>" + b.value + "</b>" ;
sXml += "</Add></soap:Body></soap:Envelope>"
// Try to parse the XML string into DOM object
xmlObj.loadXML(sXml) ;
//To see the validated XML string is well-formed
XmlRequest.innerText = xmlObj.xml ;
var xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP") ;
xmlHTTP.Open ( "Post", "http://localhost/ASP.NET/MyWebService.asmx", false) ;
xmlHTTP.setRequestHeader("SOAPAction", "http://tempuri.org/Add") ;
xmlHTTP.setRequestHeader("Content-Type", "text/xml; charset=utf-8" ) ;
xmlHTTP.Send(xmlObj.xml) ;
MyResult.innerText = xmlHTTP.responseText ;
var xmlResponse = xmlHTTP.responseXML ;
answer.innerText = xmlResponse.selectSingleNode("soap:Envelope/soap:Body/AddResponse/AddResult").text ;
}
</script>
<form>
<p>Please input a:<input id="a" name="a"></input></p>
<p>Please input b:<input id="b" name="b"></input></p>
<p>
<input type="button" id="btn" value="Enter"
onclick="jscript:btn_click(a, b)"></input>
</p>
<p>Answer is <span id="answer"></span></p>
<hr></hr>
<p>Request:</p>
<span id="XmlRequest"></span>
<p>Response:</p>
<span id="MyResult"></span>
</form>
</body>
</html>
Here I installed Microsoft XML Parser 3.0 in my client machine that give me the XMLHTTP and DOM COM objects to test my application.
No comments:
Post a Comment