Web Services are XML-defined data formats that are transmitted across system platforms through the SOAP protocol, and then the respective roles of soap and WSDL are discussed.
Soap and WSDL are the basis for a deep understanding of Web Service and WCF, so it is necessary to take some time to understand it.
First, SOAP (simple Object Access Protocol)
If we want to invoke the method of the remote object, we must tell the other person what method we are calling, and the value of the parameter of this method, etc. Then the other person returns the data to us.
This involves two questions: 1. How data is transmitted over a network. 2. How to represent data? What format to use to represent the function, its parameters, and so on.
1. Soap Transfer Protocol
The transport protocol for SOAP uses the HTTP protocol. Only the content of the HTTP transport is HTML text, and the SOAP protocol transmits SOAP data. Take a look at the following example:
This is the content of an HTTP request (requesting Google's homepage):
get/http/1.1 accept:image/gif, Image/jpeg, Image/pjpeg, Image/pjpeg, Application/x-shockwave-flash, application/ X-ms-application, APPLICATION/X-MS-XBAP, Application/vnd.ms-xpsdocument, Application/xaml+xml, application/ Vnd.ms-excel, Application/vnd.ms-powerpoint, Application/msword, */*
Accept-language:en-us
user-agent:mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; trident/4.0;. NET CLR 2.0.50727;. NET CLR 3.0.4506.2152;. NET CLR 3.5.30729; CIBA) chromeframe/4.0
Accept-encoding:gzip, deflate
Connection:keep-alive
Host:www.google.com
Cookie:pref=id=d8f9f1710bfa5f72:u=a5b3bec86b6433ef:nw=1:tm=1260238598:lm=1260241971:gm=1:s=q2agysw3bsooqmas; Nid=29=jgigddux70iqtbvannep_e9pllkbi9stjzabjgq1ewudg-_ JCGFPKA59DROC0AZKLBJ4Q77HU1VMKSCXTP3OASEYTBV643C2XPE9DS7LSXDHAKANS46VY-OU8XRQBMXJ; Rememberme=true; Sid=dqaaah4aaabw7m4nvkteor7ejumc1aj4r6hybmvewuy_uitlutzzmutpojdahuexhpa_ Epako9ex1u3r7apxz5cj28xhnv2dbfryf5ayabcimciuotitksikqn3qspgdfkrs1xn7egzdpcv0v1xflcu0erf_jfe_ D6GOGC2P2S08JNDFS9VPMW; Hsid=afeftma68egnjkbil; __utmx=173272373.; __utmxx=173272373.
---------If there is post data, there will be post data--------
This is the content of a SOAP request:
Post/webservices/weatherwebservice.asmx http/1.1
user-agent:mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.3603)
Content-type:text/xml; Charset=utf-8
soapaction: "Http://WebXml.com.cn/getSupportCity"
Host:www.webxml.com.cn
content-length:348
Expect:100-continue
Connection:keep-alive
<?xml version= "1.0" encoding= "Utf-8"? ><soap:envelope xmlns:soap= "http://schemas.xmlsoap.org/soap/ envelope/"xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:xsd=" Http://www.w3.org/2001/XMLSchema " ><soap:body><getsupportcity xmlns= "http://WebXml.com.cn/" ><byProvinceName> Guangdong </ Byprovincename></getsupportcity></soap:body></soap:envelope>
As you can see, a SOAP request is actually an HTTP request, but in order to indicate that the content is soap's data, you need to add the portion of the red word in the request above to show the difference. Other wordsIf there is soapaction in the request header, the request is processed as a soap content and is not parsed as HTML. You can specify the SOAPAction header above to indicate that the content is soap content, or you can specify content-type:application/soap+xml to indicate that the content is the content of soap. The last XML data in the SOAP request, which is the exact content of the request,This is the data format of the requested SOAP specification, which is described in detail below。
2. SOAP data format
Now that SOAP is transmitting data through the HTTP protocol's post method, it is simply that the header of the request adds some flags to indicate that it is a SOAP request. So the specific format of the data is how to specify, we put the above requested XML data to look at:
<?xml version= "1.0" encoding= "Utf-8"?>
<soap:envelope xmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:xsd=" Http://www.w3.org/2001/XMLSchema ">
<soap:Body>
<getsupportcity xmlns= "http://WebXml.com.cn/" >
<byProvinceName> Guangdong </byProvinceName>
</getSupportCity>
</soap:Body>
</soap:Envelope>
The content of the <soap:Body> inside is the content of the request, the method of the request is getsupportcity, the method has a parameter named Byprovincename, the value of the parameter is "Guangdong" this string. Take a look at what's going back:
http/1.1 OK
Date:mon, Dec 05:55:39 GMT
server:microsoft-iis/6.0
X-powered-by:asp.net
x-aspnet-version:2.0.50727
Cache-control:private, max-age=0
Content-type:text/xml; Charset=utf-8
content-length:1052
<?xml version= "1.0" encoding= "Utf-8"? ><soap:envelope xmlns:soap= "http://schemas.xmlsoap.org/soap/ envelope/"xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:xsd=" Http://www.w3.org/2001/XMLSchema " ><soap:body><getsupportcityresponse xmlns= "http://WebXml.com.cn/" ><getSupportCityResult> <string> Guangzhou (59287) </string><string> Shenzhen (59493) </string><string> Chaozhou (59312) </string ><string> Shaoguan (59082) </string><string> Zhanjiang (59658) </string><string> Huizhou (59298) </ String><string> Qingyuan (59280) </string><string> Dongguan (59289) </string><string> jiangmen (59473) </string><string> Maoming (59659) </string><string> zhaoqing (59278) </string><string> Shanwei ( 59501) </string><string> Heyuan (59293) </string><string> Jieyang (59315) </string><string > Meizhou (59117) </string><string> Zhongshan (59485) </string><string> Deqing (59269) </string>< String> Yangjiang (59663) </string><string> Yunfu (59471) </string><string> Zhuhai (59488) </string>< String> Shantou (59316) </string><string> Foshan (59279) </string></getsupportcityresult></ Getsupportcityresponse></soap:body></soap:envelope>
There is no flag in the returned HTTP header to indicate that it is a SOAP response, because it is indeed not necessary for the requestor to send out the SOAP request, and the return is definitely a SOAP response.
A typical SOAP request format is structured as follows:
<?xml version= "1.0"?>
<soap:envelope xmlns:soap= "Http://www.w3.org/2001/12/soap-envelope"
soap:encodingstyle= "Http://www.w3.org/2001/12/soap-encoding" >
<soap:Header>
<m:trans xmlns:m= "http://www.w3schools.com/transaction/"
soap:mustunderstand= "1" >234
</m:Trans>
</soap:Header>
<soap:Body>
<m:getprice xmlns:m= "Http://www.w3schools.com/prices" >
<m:Item>Apples</m:Item>
</m:GetPrice>
</soap:Body>
</soap:Envelope>
The following elements are explained individually:
A) Envelope
The request content of soap must be envelope as the root node.
Xmlns:soap= "Http://www.w3.org/2001/12/soap-envelope", cannot be modified, otherwise error will occur. Http://www.w3.org/2001/12/soap-envelope contains a definition of envelope schema. Interested can go to the content of this link.
Soap:encodingstyle= "Http://www.w3.org/2001/12/soap-encoding", which specifies the type of the data element.
b) Header
This is optional and if you need to add a header element, it must be the first element of the envelope.
The contents of the header are not strictly limited, we can add some of their own application-related content, but the client must remember to handle these header elements, you can add mustunderstand force processing.
c) Body
This is the subject matter of the request, what function is requested, what type of parameter it is, etc. are specified in this context.
A label is used to represent a function, and then a child element is used to represent its parameters.
There is no parameter and return type specified in the call, as there is no need to specify it because the party providing the service has already defined the data type, and specifying the data type at the time of invocation has no meaning.
Ii. WSDL (Web Services Description Language)
WSDL is used to describe WebService, which describes in XML the WebService methods, parameter types, access paths, and so on. We're going to use a webservice to get its WSDL first, and when we add a Web reference to VS, it's done by the development environment, and the development environment generates the appropriate proxy class for us to use from the WSDL document to the Web service.
The following is a HelloWorld WebService service-side code:
public class Service:System.Web.Services.WebService
{
Public Service () {
Uncomment the following line if using designed components
InitializeComponent ();
}
[WebMethod]
Public DateTime HelloWorld (int i)
{
return datetime.now;
}
}
The WSDL document for its corresponding WebService is as follows:
1 <?xml version= "1.0" encoding= "Utf-8"?>
2 <wsdl:definitions xmlns:soap= "http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm= "http://microsoft.com/wsdl/ mime/textmatching/"xmlns:soapenc=" http://schemas.xmlsoap.org/soap/encoding/"xmlns:mime="/HTTP/ schemas.xmlsoap.org/wsdl/mime/"xmlns:tns=" http://tempuri.org/"xmlns:s=" Http://www.w3.org/2001/XMLSchema "xmlns: Soap12= "http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http= "http://schemas.xmlsoap.org/wsdl/http/" Targetnamespace= "http://tempuri.org/" xmlns:wsdl= "http://schemas.xmlsoap.org/wsdl/" >
3 <wsdl:types>
4 <s:schema elementformdefault= "qualified" targetnamespace= "http://tempuri.org/" >
5 <s:element name= "HelloWorld" >
6 <s:complexType>
7 <s:sequence>
8 <s:element minoccurs= "1" maxoccurs= "1" name= "I" type= "S:int"/>
9 </s:sequence>
Ten </s:complexType>
</s:element>
<s:element name= "Helloworldresponse" >
<s:complexType>
<s:sequence>
<s:element minoccurs= "1" maxoccurs= "1" name= "Helloworldresult" type= "S:datetime"/>
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message name= "Helloworldsoapin" >
<wsdl:part name= "Parameters" element= "Tns:helloworld"/>
</wsdl:message>
<wsdl:message name= "Helloworldsoapout" >
<wsdl:part name= "Parameters" element= "Tns:helloworldresponse"/>
</wsdl:message>
<wsdl:porttype name= "Servicesoap" >
<wsdl:operation name= "HelloWorld" >
<wsdl:input message= "Tns:helloworldsoapin"/>
<wsdl:output message= "Tns:helloworldsoapout"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name= "Servicesoap" type= "Tns:servicesoap" >
<soap:binding transport= "Http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name= "HelloWorld" >
<soap:operation soapaction= "Http://tempuri.org/HelloWorld" style= "Document"/>
Panax Notoginseng <wsdl:input>
<soap:body use= "literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use= "literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name= "SERVICESOAP12" type= "Tns:servicesoap" >
<soap12:binding transport= "Http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name= "HelloWorld" >
<soap12:operation soapaction= "Http://tempuri.org/HelloWorld" style= "Document"/>
<wsdl:input>
<soap12:body use= "literal"/>
Wuyi </wsdl:input>
<wsdl:output>
<soap12:body use= "literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name= "Service" >
<wsdl:port name= "Servicesoap" binding= "Tns:servicesoap" >
<soap:address location= "Http://localhost:2206/WebSite1/Service.asmx"/>
</wsdl:port>
<wsdl:port name= "SERVICESOAP12" binding= "TNS:SERVICESOAP12" >
<soap12:address location= "Http://localhost:2206/WebSite1/Service.asmx"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
A WSDL document consists of four parts:
1, types
Specifies all data types used by WebService, with two data types, int and datetime
2. Message
Indicates the data type used by an operation.
Helloworldsoapin refers to the data type used for the input operation of HelloWorld, helloworldsoapout refers to the data type used for the output operation of HelloWorld. The element elements of both indicate the specific type that corresponds to the types.
3, PortType
This webservice all supported operations, that is, what methods are available for invocation.
Here, a HelloWorld call is supported, and its inputs and outputs correspond to the two data types of Helloworldsoapin and helloworldsoapout.
4. Binding
The transport of the Soap12:binding element indicates the transport protocol, here is the HTTP protocol.
Operation indicates the action to be exposed to the outside call.
The Use property specifies the encoding of the input and output, and no encoding is specified here.
5. Services
Specifies some information about the service, primarily the access path to the specified service.
Some essential knowledge of XML, SOAP, and WSDL in Web Services