basicHttpBinding,HTTP-GET,SOAP

一、WCF服务

写了一个简单的WCF服务,配置文件表明,本服务采用basicHttpBinding,即采用Http协议传输SOAP消息,采用HTTP-GET协议提供元数据。

服务契约:

服务契约
 1 [ServiceContract]
 2 public interface Ihttpsoap
 3 {
 4      [OperationContract]
 5      String HelloWorldFromHttpSOAP();
 6 }
 7 
 8 
 9 public class httpsoap : Ihttpsoap
10 {
11      publicString HelloWorldFromHttpSOAP()
12      {
13           return"Hello,world";
14      }
15 }

配置文件(httpGetUrl不是必须的,如果不用该属性,则用加?wsdl后缀的方式访问元数据):

配置文件
 1 <?xmlversion="1.0"encoding="utf-8" ?>
 2 <configuration > 
 3   <system.serviceModel >
 4      <behaviors > 
 5             <serviceBehaviors > 
 6              <behavior name="metadataBehavior" > 
 7                   <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8733/httpsoap/metadata" />
 8              </behavior > 
 9          </serviceBehaviors > 
10      </behaviors > 
11      <services > 
12          <service behaviorConfiguration="metadataBehavior" name="testwcf.serviers.httpsoap" > 
13               <host > 
14                     <baseAddresses > 
15                          <add baseAddress="http://localhost:8733/httpsoap"/>
16                     </baseAddresses > 
17                </host > 
18               <endpoint address="" binding="basicHttpBinding" contract="testwcf.serviers.Ihttpsoap" />
19          </service > 
20      </services > 
21   </system.serviceModel > 
22 
23 </configuration > 

宿主:

宿主
 1 static void Main(string[] args)
 2 {
 3      using (ServiceHost host = newServiceHost(typeof(httpsoap)))
 4      {
 5           host.Opened += delegate
 6           {
 7                Console.WriteLine("httpsoap已经启动,按任意键终止服务!");
 8           };
 9 
10           host.Open();
11           Console.Read();
12      }
13 }

二、加载服务

使用WCF Test Client加载http://leiwangs-pc:8733/httpsoap/metadata (注意Fiddler对localhost无法截获)。

加载http://leiwangs-pc:8733/httpsoap 后的Request是一致的,Response不太一样,因为前者是对元数据的访问(也可使用加?wsdl后缀的方式访问),后者是访问服务。

另外,请注意使用WCF Test Client时需要修改Config File中的内容,将地址中的localhost修改为机器名或IP,如下图:

如下图上面的黑框框住的是本次截获的数据。

从截图可以看出,加载的时候先POST到/httpsoap/metadata,然后POST到/httpsoap/metadata/mex,最后从/httpsoap/metadata处GET,返回元数据。(前两次的POST的用途是什么?

  • POST到/httpsoap/metadata (到mex基本上是一样的),可以看出响应都是404 Not Found。

Request(RAW):
 1 Request(RAW):
 2 
 3 POST http://leiwangs-pc:8733/httpsoap/metadata HTTP/1.1
 4 Content-Type: application/soap+xml; charset=utf-8
 5 Host: leiwangs-pc:8733
 6 Content-Length: 483
 7 Expect: 100-continue
 8 Accept-Encoding: gzip, deflate
 9 Connection: Keep-Alive
10 
11 <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">     
12      <s:Header>
13           <a:Action s:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2004/09/transfer/Get</a:Action>     
14           <a:MessageID>urn:uuid:9516ca19-f143-4b2f-8135-07492bebb362</a:MessageID>
15           <a:ReplyTo>
16                <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
17           </a:ReplyTo>
18           <a:To s:mustUnderstand="1">http://leiwangs-pc:8733/httpsoap/metadata</a:To>
19      </s:Header>
20      <s:Body/>
21 </s:Envelope>
Response (RAW):
1 HTTP/1.1 404 Not Found
2 Content-Length: 0
3 Server: Microsoft-HTTPAPI/2.0
4 
5 Date: Thu, 11 Apr 2013 01:41:30 GMT
  • 从/httpsoap/metadata处GET

Request(RAW):
1 GET http://leiwangs-pc:8733/httpsoap/metadata HTTP/1.1
2 User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.17929)
3 
4 Host: leiwangs-pc:8733
Response(RAW):Header
1 HTTP/1.1 200 OK
2 Content-Length: 2652
3 Content-Type: text/xml; charset=UTF-8
4 Server: Microsoft-HTTPAPI/2.0
5 
6 Date: Thu, 11 Apr 2013 01:41:30 GMT
Response(RAW):Content, metadata
 1  <?xml version="1.0" encoding="utf-8" ?>
 2 - <wsdl:definitions name="httpsoap" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:tns="http://tempuri.org/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata">
 3 - <wsdl:types>
 4 - <xsd:schema targetNamespace="http://tempuri.org/Imports">
 5   <xsd:import schemaLocation="http://localhost:8733/httpsoap/metadata?xsd=xsd0" namespace="http://tempuri.org/" />
 6   <xsd:import schemaLocation="http://localhost:8733/httpsoap/metadata?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
 7   </xsd:schema>
 8   </wsdl:types>
 9 - <wsdl:message name="Ihttpsoap_HelloWorldFromHttpSOAP_InputMessage">
10   <wsdl:part name="parameters" element="tns:HelloWorldFromHttpSOAP" />
11   </wsdl:message>
12 - <wsdl:message name="Ihttpsoap_HelloWorldFromHttpSOAP_OutputMessage">
13   <wsdl:part name="parameters" element="tns:HelloWorldFromHttpSOAPResponse" />
14   </wsdl:message>
15 - <wsdl:portType name="Ihttpsoap">
16 - <wsdl:operation name="HelloWorldFromHttpSOAP">
17   <wsdl:input wsaw:Action="http://tempuri.org/Ihttpsoap/HelloWorldFromHttpSOAP" message="tns:Ihttpsoap_HelloWorldFromHttpSOAP_InputMessage" />
18   <wsdl:output wsaw:Action="http://tempuri.org/Ihttpsoap/HelloWorldFromHttpSOAPResponse" message="tns:Ihttpsoap_HelloWorldFromHttpSOAP_OutputMessage" />
19   </wsdl:operation>
20   </wsdl:portType>
21 - <wsdl:binding name="BasicHttpBinding_Ihttpsoap" type="tns:Ihttpsoap">
22   <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
23 - <wsdl:operation name="HelloWorldFromHttpSOAP">
24   <soap:operation soapAction="http://tempuri.org/Ihttpsoap/HelloWorldFromHttpSOAP" style="document" />
25 - <wsdl:input>
26   <soap:body use="literal" />
27   </wsdl:input>
28 - <wsdl:output>
29   <soap:body use="literal" />
30   </wsdl:output>
31   </wsdl:operation>
32   </wsdl:binding>
33 - <wsdl:service name="httpsoap">
34 - <wsdl:port name="BasicHttpBinding_Ihttpsoap" binding="tns:BasicHttpBinding_Ihttpsoap">
35   <soap:address location="http://localhost:8733/httpsoap" />
36   </wsdl:port>
37   </wsdl:service>
38   </wsdl:definitions>

从中可以看出,本服务Host在控制台中,所以Server是Microsoft-HTTPAPI。另外元数据是以WSDL形式体现的。

The HTTP Server API enables applications to communicate over HTTP without using Microsoft Internet Information Server (IIS). Applications can register to receive HTTP requests for particular URLs, receive HTTP requests, and send HTTP responses. The HTTP Server API includes SSL support so that applications can exchange data over secure HTTP connections without IIS. It is also designed to work with I/O completion ports.

 

WSDL是Web Services Description Language的缩写,是一个用来描述Web服务和说明如何与Web服务通信的XML语言。

三、调用服务提供的方法

使用WCF Test Client Invoke HelloWorldFromHttpSOAP方法,Fiddler截获的消息如下:

Request(RAW):
 1 POST http://leiwangs-pc:8733/httpsoap HTTP/1.1
 2 Content-Type: text/xml; charset=utf-8
 3 SOAPAction: "http://tempuri.org/Ihttpsoap/HelloWorldFromHttpSOAP"
 4 Host: leiwangs-pc:8733
 5 Content-Length: 147
 6 Expect: 100-continue
 7 Accept-Encoding: gzip, deflate
 8 Connection: Keep-Alive
 9 
10 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
11      <s:Body>
12           <HelloWorldFromHttpSOAP xmlns="http://tempuri.org/"/>
13      </s:Body>
14 </s:Envelope>

从中可以看出调用服务提供的方法,就是向服务发送一个POST请求,并且指明了SOAPAction。信封里没有Header,只有Body,Body里是请求的方法名称。

Response(RAW):
 1 HTTP/1.1 200 OK
 2 Content-Length: 259
 3 Content-Type: text/xml; charset=utf-8
 4 Server: Microsoft-HTTPAPI/2.0
 5 Date: Thu, 11 Apr 2013 01:42:57 GMT
 6 
 7 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
 8      <s:Body>
 9           <HelloWorldFromHttpSOAPResponse xmlns="http://tempuri.org/">                    
10                <HelloWorldFromHttpSOAPResult>Hello,world</HelloWorldFromHttpSOAPResult>     
11           </HelloWorldFromHttpSOAPResponse>
12      </s:Body>
13 </s:Envelope>

从中可以看出信封里有一个Body,之内为Response,再里面为Result。

值得注意的是请求和响应的内容全部是明文,这种做法并不安全。baseHttpBinding这种方式可以做安全设置,之后研究WCF安全时再深入。

posted @ 2013-04-11 18:08  BlueMountain_79  阅读(547)  评论(0编辑  收藏  举报