WebService、WcfService和WcfRestService数据包的格式总结

来源:互联网 发布:酷狗总提示网络异常 编辑:程序博客网 时间:2024/05/22 13:50
 

WebService、WcfService和WcfRestService数据包的格式总结

准备工作:

使用VS 2010分别创建Asp.net WebService工程,WcfService工程,WcfRestService工程(工程模版需要在线下载,名称为“WCF REST Service Template 40(CS)”)和既支持WSDL又支持Rest的WcfService(创建方法会在下篇文章中介绍),然后分别在这四个工程中创建CloneObject接口,其实现如下:

        public List<CompositeType> CloneObject(CompositeType composite)        {            if (composite == null)            {                throw new ArgumentNullException("composite");            }            List<CompositeType> lst = new List<CompositeType>();            lst.Add(composite);            lst.Add(new CompositeType() { BoolValue=composite.BoolValue, StringValue="Clone " + composite.StringValue});            return lst;        }


相关类型的定义(注意:在WcfService中需要使用DataContract和DataMember进行接口契约的约定):

    public class CompositeType    {        bool boolValue = true;        string stringValue = "Hello ";        public bool BoolValue        {            get { return boolValue; }            set { boolValue = value; }        }        public string StringValue        {            get { return stringValue; }            set { stringValue = value; }        }    }


  

 

工具准备:

1、SmartSniff来抓取网络数据包。

2、RESTClient工具来手工模拟Soap和Rest数据包,下载地址:http://ishare.iask.sina.com.cn/f/14712254.html?from=like

 

一、Asp.net WebService的收发格式:

POST /Service1.asmx HTTP/1.1

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.4963)

VsDebuggerCausalityData: uIDPo/kvik7siYRDlyTVRRP2DXgAAAAA1x0n85jS9EKUZEVmdJtUNXyQP/eirp9HqXBHCljHLvsACQAA

Content-Type: text/xml; charset=utf-8

SOAPAction: "http://tempuri.org/CloneObject"

Host: 192.168.2.249:8081

Content-Length: 383

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><CloneObject xmlns="http://tempuri.org/"><composite><BoolValue>true</BoolValue><StringValue>Hello</StringValue></composite></CloneObject></soap:Body></soap:Envelope>

 

 

HTTP/1.1 200 OK

Cache-Control: private, max-age=0

Content-Type: text/xml; charset=utf-8

Server: Microsoft-IIS/7.5

X-AspNet-Version: 2.0.50727

X-Powered-By: ASP.NET

Date: Fri, 25 Nov 2011 04:43:59 GMT

Content-Length: 542

 

<?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><CloneObjectResponse xmlns="http://tempuri.org/"><CloneObjectResult><CompositeType><BoolValue>true</BoolValue><StringValue>Hello</StringValue></CompositeType><CompositeType><BoolValue>true</BoolValue><StringValue>Clone Hello</StringValue></CompositeType></CloneObjectResult></CloneObjectResponse></soap:Body></soap:Envelope>

 

小结:

1、把Request Header中的Content-Type去掉,返回“415.0 - Unsupported Media Type”错误。

2、把Request Header中的Content-Type替换为text/json; charset=utf-8,同时把Request Body的内容也替换为json格式,返回“415.0 - Unsupported Media Type”错误。

3、Request Header中的Content-Type和SOAPAction为必须项。

 

 

二、WcfService的收发格式:

POST /Service1.svc HTTP/1.1

Content-Type: text/xml; charset=utf-8

VsDebuggerCausalityData: uIDPozltUZVMCbxJnLU2jDSCQ9gAAAAA5T+R78gzqEmLlNDW+ZxgWRlMOMdmhblGrSJZCBz+nmYACQAA

SOAPAction: "http://tempuri.org/IService1/CloneObject"

Host: 192.168.2.249:8082

Content-Length: 357

Expect: 100-continue

Accept-Encoding: gzip, deflate

 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><CloneObject xmlns="http://tempuri.org/"><composite xmlns:a="http://schemas.datacontract.org/2004/07/SharpWcfService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:BoolValue>true</a:BoolValue><a:StringValue>Hello</a:StringValue></composite></CloneObject></s:Body></s:Envelope>

 

 

HTTP/1.1 200 OK

Content-Length: 532

Content-Type: text/xml; charset=utf-8

Server: Microsoft-IIS/7.5

X-Powered-By: ASP.NET

Date: Fri, 25 Nov 2011 05:34:56 GMT

 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><CloneObjectResponse xmlns="http://tempuri.org/"><CloneObjectResult xmlns:a="http://schemas.datacontract.org/2004/07/SharpWcfService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:CompositeType><a:BoolValue>true</a:BoolValue><a:StringValue>Hello</a:StringValue></a:CompositeType><a:CompositeType><a:BoolValue>true</a:BoolValue><a:StringValue>Clone Hello</a:StringValue></a:CompositeType></CloneObjectResult></CloneObjectResponse></s:Body></s:Envelope>

 

 

小结:

1、把Request Header中的Content-Type去掉,返回“415.0 - Unsupported Media Type”错误。

2、把Request Header中的Content-Type替换为text/json; charset=utf-8,同时把Request Body的内容也替换为json格式,返回“415.0 - Unsupported Media Type”错误(WebInvoke的RequestFormat和ResponseFormat的设置在此都无效,如何支持json格式?)。

3、Request Header中的Content-Type和SOAPAction为必须项。

 

 

三、WcfRestService的收发格式:

 

POST /Service1/CloneObject HTTP/1.1

Content-Type: text/json; charset=utf-8

Content-Length: 39

Host: 192.168.2.249:8083

Connection: Keep-Alive

User-Agent: Apache-HttpClient/4.0.1 (java 1.5)

Expect: 100-Continue

 

{"BoolValue":true,"StringValue":"test"}

 

 

HTTP/1.1 200 OK

Cache-Control: private

Content-Length: 87

Content-Type: text/json; charset=utf-8

Server: Microsoft-IIS/7.5

X-AspNet-Version: 4.0.30319

X-Powered-By: ASP.NET

Date: Fri, 25 Nov 2011 05:57:03 GMT

 

[{"BoolValue":true,"StringValue":"test"},{"BoolValue":true,"StringValue":"Clone test"}]

 

改用xml格式请求该WecRestService

POST /Service1/CloneObject HTTP/1.1

Content-Type: text/xml; charset=utf-8

Content-Length: 210

Host: 192.168.2.249:8083

Connection: Keep-Alive

User-Agent: Apache-HttpClient/4.0.1 (java 1.5)

Expect: 100-Continue

 

<CompositeType xmlns="http://schemas.datacontract.org/2004/07/SharpWcfRestService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><BoolValue>true</BoolValue><StringValue>Hello</StringValue></CompositeType>

 

HTTP/1.1 200 OK

Cache-Control: private

Content-Length: 351

Content-Type: text/xml; charset=utf-8

Server: Microsoft-IIS/7.5

X-AspNet-Version: 4.0.30319

X-Powered-By: ASP.NET

Date: Fri, 25 Nov 2011 06:01:21 GMT

 

<ArrayOfCompositeType xmlns="http://schemas.datacontract.org/2004/07/SharpWcfRestService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><CompositeType><BoolValue>true</BoolValue><StringValue>Hello</StringValue></CompositeType><CompositeType><BoolValue>true</BoolValue><StringValue>Clone Hello</StringValue></CompositeType></ArrayOfCompositeType>

 

 

小结:

1、把Request Header中的Content-Type去掉,返回“400 Bad Request”错误。

2、把Request Header中的Content-Type替换为text/json; charset=utf-8,同时把Request Body的内容也替换为json格式,同样能正确的返回结果。

3、Request Header中的Content-Type为必须项。

 

 

 

四、支持RESTful的WcfService的收发格式:

使用VS.NET生成的代理类的访问格式(Soap格式):

POST /Service1.svc HTTP/1.1

Content-Type: text/xml; charset=utf-8

VsDebuggerCausalityData: uIDPo37ZghDy+BNMsXzQ+pMEz2cAAAAAimYhgnq7uE2jaYbqWfU0WpBBztReWUBNhPN3rCdqMOcACQAA

SOAPAction: "http://tempuri.org/IService1/CloneObject"

Host: 192.168.2.249:8084

Content-Length: 364

Expect: 100-continue

Accept-Encoding: gzip, deflate

Connection: Keep-Alive

 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><CloneObject xmlns="http://tempuri.org/"><composite xmlns:a="http://schemas.datacontract.org/2004/07/WcfService2RestService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:BoolValue>true</a:BoolValue><a:StringValue>Hello</a:StringValue></composite></CloneObject></s:Body></s:Envelope>

 

 

HTTP/1.1 200 OK

Cache-Control: private

Content-Length: 539

Content-Type: text/xml; charset=utf-8

Server: Microsoft-IIS/7.5

X-AspNet-Version: 4.0.30319

X-Powered-By: ASP.NET

Date: Fri, 25 Nov 2011 06:21:17 GMT

 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><CloneObjectResponse xmlns="http://tempuri.org/"><CloneObjectResult xmlns:a="http://schemas.datacontract.org/2004/07/WcfService2RestService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:CompositeType><a:BoolValue>true</a:BoolValue><a:StringValue>Hello</a:StringValue></a:CompositeType><a:CompositeType><a:BoolValue>true</a:BoolValue><a:StringValue>Clone Hello</a:StringValue></a:CompositeType></CloneObjectResult></CloneObjectResponse></s:Body></s:Envelope>

 

 

 

使用Rest客户端的请求格式(Rest格式):

POST /Service1/CloneObject HTTP/1.1

Content-Type: text/json; charset=utf-8

Content-Length: 39

Host: 192.168.2.249:8084

Connection: Keep-Alive

User-Agent: Apache-HttpClient/4.0.1 (java 1.5)

Expect: 100-Continue

 

{"BoolValue":true,"StringValue":"test"}

 

HTTP/1.1 200 OK

Cache-Control: private

Content-Length: 87

Content-Type: text/json; charset=utf-8

Server: Microsoft-IIS/7.5

X-AspNet-Version: 4.0.30319

X-Powered-By: ASP.NET

Date: Fri, 25 Nov 2011 06:27:45 GMT

 

[{"BoolValue":true,"StringValue":"test"},{"BoolValue":true,"StringValue":"Clone test"}]

 

Rest客户端(指定Content-Type为xml)

POST /Service1/CloneObject HTTP/1.1

Content-Type: text/xml; charset=utf-8

Content-Length: 212

Host: 192.168.2.249:8084

Connection: Keep-Alive

User-Agent: Apache-HttpClient/4.0.1 (java 1.5)

Expect: 100-Continue

 

<CompositeType xmlns="http://schemas.datacontract.org/2004/07/WcfService2RestService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><BoolValue>true</BoolValue><StringValue>test</StringValue></CompositeType>

 

HTTP/1.1 200 OK

Cache-Control: private

Content-Length: 352

Content-Type: text/xml; charset=utf-8

Server: Microsoft-IIS/7.5

X-AspNet-Version: 4.0.30319

X-Powered-By: ASP.NET

Date: Fri, 25 Nov 2011 06:29:57 GMT

 

<ArrayOfCompositeType xmlns="http://schemas.datacontract.org/2004/07/WcfService2RestService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><CompositeType><BoolValue>true</BoolValue><StringValue>test</StringValue></CompositeType><CompositeType><BoolValue>true</BoolValue><StringValue>Clone test</StringValue></CompositeType></ArrayOfCompositeType>

 

小结:

1、该服务支持Soap和Rest两种格式访问。

2、Rest格式访问的时候,同时支持XML和JSON格式,Request Header中的Content-Type为必须项。

3、Soap格式访问的时候,Request Header中的Content-Type和SOAPAction为必须项。 

 

 

 

 

 

 

 

原创粉丝点击