如何测试WCF Rest

使用SoapUI

1.新建一个rest项目

 

2.双击上图中的Request1

查询的时候,Method选择post

resource的地方要调整到对应的方法

查询的内容是用json格式发送

查询的的结果使用json查看

 

 Endpoint需要补充二级域名

 

层级结构说明

第1个是project

第2个是wsdl或者wadl,对应到某一个域名

第3个是Service,比如在一个域名下部署多个service

第4个是Method,是service下的某一个具体的方法

第5个是Request,一个method可以有多个request,每个request可以设置自己独立的参数

 

 

 

使用Fiddler

https://stackoverflow.com/questions/7273364/wcf-rest-service-post-method-fails-in-fiddler

执行post

Content-Type: application/json

post下面的方框填写内容的格式,RequestBody填写传输的内容,操作类型可以选择为post,右上角execute

 

执行结果,在inspectors中查看

 

request1

POST http://localhost/Chile.Api/chileservice.svc/GetAllPartnershipsWithBrands HTTP/1.1
User-Agent: Fiddler
Host: localhost
Content-Length: 0

response1  这里的处理是有问题是,应该是返回一个未授权的错误

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/10.0
Set-Cookie: ASP.NET_SessionId=wjwnpsjnewi34hxtwr1php4b; path=/; HttpOnly
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 18 Jan 2019 08:48:38 GMT
Content-Length: 46

{"Message":"Authentication Error","Status":98}

 

request2

手动添加这个 Content-Type: application/json

POST http://localhost/Chile.Api/chileservice.svc/GetAllPartnershipsWithBrands HTTP/1.1
User-Agent: Fiddler
Host: localhost
Content-Length: 138
Content-Type: application/json

{
"header":
{
"SecurityHash":"5b1f979e06ed3e4c7e6c92029d35ba93b96b5ab0131d1f6d8306aa04ad7abe43"
},
"parameters":
{
"Key":"xxx"
} }

response2 

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/10.0
Set-Cookie: ASP.NET_SessionId=bsvtlc0sqaqxm3zcsdrfw0od; path=/; HttpOnly
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 18 Jan 2019 09:17:27 GMT
Content-Length: 98

{"Message":"Server error. Detail: There is no row at position 0.","Status":99,"Partnerships":null}

request3

POST http://localhost/Chile.Api/chileservice.svc/GetAllPartnershipsWithBrands HTTP/1.1
User-Agent: Fiddler
Host: localhost
Content-Length: 138
Content-Type: application/json

{
"header":
{
"SecurityHash":"5b1f979e06ed3e4c7e6c92029d35ba93b96b5ab0131d1f6d8306aa04ad7abe43"
},
"parameters":
{
"Key":"xxx"
} }

response3 

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
Set-Cookie: ASP.NET_SessionId=hjxujm3vjc2pplf3zzg20a00; path=/; HttpOnly
X-Powered-By: ASP.NET
Date: Fri, 18 Jan 2019 09:55:21 GMT
Content-Length: 611

{"Message":"","Status":0,"Partnerships":[{"Brands":"Brand1;Brand2;Brand3;Brand4;Brand5","PartnershipName":"Partnership1","PartnershipRUT":"Partnership1"},{"Brands":"Babytuto","PartnershipName":"Babytuto","PartnershipRUT":"76.268.895-6"},{"Brands":"Brand1","PartnershipName":"Depto51","PartnershipRUT":"76.319.368-3"},{"Brands":"Cencosud;Paris;Johnson;Vuelo Parapente;Varo's R'sestobar;Falabella","PartnershipName":"Regalbox","PartnershipRUT":"76.108.976-5"},{"Brands":"Brand1","PartnershipName":"Falabella","PartnershipRUT":"76.142.721-0"},{"Brands":"LATAM","PartnershipName":"LATAM","PartnershipRUT":"LATAM"}]}

 

 

 

直接新建一个控制台应用,然后添加WCF的Contract类库作为引用【添加ServiceReference会出问题的】

复制代码
    class Program
    {
        private static string url = "http://172.31.212.20/Lisa.WebApi2/ChileService.svc";

        static void Main(string[] args)
        {
            //设置EmptyRequest的key
            EmptyRequest emptyRequest = new EmptyRequest { Key = "xxx" };
            //对emptyRequest进行Json序列化
            string strJson = JsonConvert.SerializeObject(emptyRequest);

            //进行加密
            var securityKey = "ChuckLu";
            HMACSHA256Encryption hmacsha256Encryption = new HMACSHA256Encryption();
            var securityHash = hmacsha256Encryption.Encrypt(strJson, securityKey);

            //将加密结果封装为messageHeader
            MessageHeader messageHeader = new MessageHeader { SecurityHash = securityHash };


            ChannelFactory<IChileService> cf = new ChannelFactory<IChileService>(new WebHttpBinding(), url);
            cf.Endpoint.Behaviors.Add(new WebHttpBehavior());
            IChileService channel = cf.CreateChannel();

            //调用
            PartnershipListResponse partnershipListResponse =
                channel.GetAllPartnershipsWithBrands(messageHeader, emptyRequest);

            if (partnershipListResponse.Status != OperationStatus.Ok)
            {
                Console.WriteLine(partnershipListResponse.Status);
                Console.ReadKey();
            }

            strJson = JsonConvert.SerializeObject(partnershipListResponse);
            Console.WriteLine(strJson);

            //foreach (var item in partnershipListResponse.Partnerships)
            //{
            //    Console.WriteLine($@"{item.PartnershipRUT} {item.PartnershipName} {item.Brands}{Environment.NewLine}");
            //}
            Console.ReadKey();
        }
    }
复制代码

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(841)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示