The concept of soap should not be a novelty. To put it simply, soap is a standard that combines data in XML, then through the HTTP protocol (or other protocol, but usually always with the HTTP protocol) and the remote service, or it can be considered a middleware that serves as a bridge for communication. Because when all services use the same standard, communication is easier.
Admittedly, the SOAP-formatted message content is redundant and relies on a well-defined XML schemas, which is complex to create SOAP messages manually and requires tools to simplify the work. As a result, more and more Web services tend to use restful style webservice.
According to the SOAP protocol, only a valid SOAP message needs to be sent to the server to enable communication. So how do I generate a valid SOAP message? The SOAP Official document details the format of soap, but the official documentation is long, and the average person is impatient to read it, most of the time just check it out when needed, or go to http://w3school.com.cn/soap/soap_ Syntax.asp Learn the simplified version. Here is a tool, SoapUI, can go to its official website http://www.soapui.org/ Download, it can generate the requested SOAP message format through the WSDL.
Asp. NET, the Web service created with the wizard is SOAP-style. Suppose we create a Web service. Using the wizard, complete the following code:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebService1
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace =
"http://tempuri.org/"
)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(
false
)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World"
;
}
[WebMethod]
public int Add(
int a,
int b)
{
return a + b;
}
/// <summary>
/// 把公制单位的汽车转化成以英制单位表示的汽车
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
[WebMethod]
public car ChangeCarUnit(car s)
{
s.length = s.length * 3.3m;
s.width = s.width * 3.3m;
s.weight = s.width * 2.2m;
return s;
}
}
public class car
{
public string model =
""
;
public decimal length = 0;
public decimal width = 0;
public decimal weight = 0;
}
}
|
The method is written in the Webservice1.asmx file, and the SOAP message format can be obtained through the WSDL of the Web service. These two use SOAPUI to enter the specified WSDL file, that is, the SOAP message format can be generated automatically.
Note: in ASP. NET, you can access the Webservice1.asmx and enter the query string? WSDL, the WSDL file can be obtained by entering webservice1.asmx?wsdl in IE browser.
It is also important to note that the WSDL file generated by Microsoft has 2 binding, representing soap1.1 and soap1.2, which are supported.
So in the SOAPUI can see 2 different WebService interface, actually is similar.
Double-click Add request to get the SOAP message format, where the question mark can enter the specified value, and then click the Execute button, you can get the response of the SOAP message format.
After the SOAP message is generated by SOAPUI, you can construct the SOAP message yourself to invoke the soap-style webservice, so as long as you resolve how to generate the requested SOAP message, we can even implement a Web service invocation framework ourselves, whether PHP-based, ASP, or JavaScript.
-----------------------------------------------------------------------
The following shows how to send soap in JavaScript.
Before the code calls the method Changecarunit in WebService1, this method converts the car parameters from metric units to imperial units.
The SOAP message format is first obtained manually through SOAPUI. Generate and replenish data to get the following format
123456789101112131415 |
<
soapenv:Envelope xmlns:soapenv
=
"http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem
=
"http://tempuri.org/"
>
<
soapenv:Header
/>
<
soapenv:Body
>
<
tem:ChangeCarUnit
>
<!--Optional:-->
<
tem:s
>
<!--Optional:-->
<
tem:model
>Passat</
tem:model
>
<
tem:length
>4.87</
tem:length
>
<
tem:width
>1.834</
tem:width
>
<
tem:weight
>1435</
tem:weight
>
</
tem:s
>
</
tem:ChangeCarUnit
>
</
soapenv:Body
>
</
soapenv:Envelope
>
|
So just send this string of XML to WebService.
Implemented with jquery Ajax.
The code is as follows:
123456789101112131415161718192021222324252627282930313233 |
<script type=
"text/javascript"
>
$(
function () {
$(
"#btnclick"
).click(
function () {
var soapmessage =
""
;
soapmessage +=
‘<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">‘
;
soapmessage +=
‘<soapenv:Header/>‘
;
soapmessage +=
‘<soapenv:Body>‘
;
soapmessage +=
‘<tem:ChangeCarUnit>‘
;
soapmessage +=
‘ <!--Optional:-->‘
;
soapmessage +=
‘ <tem:s>‘
;
soapmessage +=
‘ <!--Optional:-->‘
;
soapmessage +=
‘ <tem:model>Passat</tem:model>‘
;
soapmessage +=
‘ <tem:length>4.87</tem:length>‘
;
soapmessage +=
‘ <tem:width>1.834</tem:width>‘
;
soapmessage +=
‘ <tem:weight>1435</tem:weight>‘
;
soapmessage +=
‘ </tem:s>‘
;
soapmessage +=
‘</tem:ChangeCarUnit>‘
;
soapmessage +=
‘ </soapenv:Body>‘
;
soapmessage +=
‘</soapenv:Envelope>‘
;
var option = {
url:
‘http://localhost:28689/WebService1.asmx‘
,
type:
‘POST‘
,
contentType:
‘text/xml‘
,
success:
function (result) {
alert(result.documentElement.textContent);
},
data: soapmessage
};
$.ajax(option);
});
});
</script>
<input value=
‘click‘ type=
"button" id=
"btnclick" />
|
When the button is clicked, the SOAP message is post to the Web server, and the return message is returned, and the message is also XML-based text.
Through the above example, we can write a dedicated tool to generate the SOAP message, through encapsulation, and then through the Post method (such as C # HttpWebRequest) to implement the Webserverice call.
This article is from "a blog" blog, make sure to keep this source http://cnn237111.blog.51cto.com/2359144/1357029
(go) The invocation principle of soap-based WebService