如何测试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();
}
}
如何测试WCF Rest的更多相关文章
- 测试WCF遇到的一些问题
win7+iis7 1.localhost访问bad request错误. 主机地址不要指定为127.0.0.1.设置为”全部未分配“. 2.错误 500.19(由于权限不足而无法读取配置文件)的问题 ...
- 使用WCF 测试客户端测试你的WCF服务
wcftestclient.exe是一个GUI的工具用于测试WCF,只需在Visual studio command line 窗口中键入 wcftestclient,就启动这个程序.如下图: 然后通 ...
- 使用WCF测试客户端 z
http://blog.csdn.net/u013036274/article/details/50570989 [是什么] WCF测试客户端(WCF Test Client)是一个用来测试WCF服务 ...
- Entity Framework 6 Recipes 2nd Edition(9-2)译->用WCF更新单独分离的实体
9-2. 用WCF更新单独分离的实体 问题 你想通过WCF为一个数据存储发布查询,插入,删除和修改,并且使这些操作尽可能地简单 此外,你想通过Code First方式实现EF6的数据访问管理 解决方案 ...
- WCF的同步和异步(以WPF连接为例)
2016-06-0711:05:44 在学习WCF时,学到WCF服务的同步和异步. 我理解的同步是: 当WCF服务是同步执行时,程序只有一条线程,代码只能按顺序一步一步来执行,当执行客户端/服务端某方 ...
- WCF入门简单教程(图文) VS2010版
在这个例子中我们将使用VS 2010 创建一个WCF服务,其中会了解 [DataContract] [ServiceContract] 等特性. 内置的 WCFSVCHost ,并使用“WCF测试客 ...
- WCF入门
一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...
- wcf第1步
添加System.ServiceModel 引用 Wcf 服务端 class Program { static void Main(string[] args) { ServiceHost host ...
- 【WCF】无废话WCF入门教程
一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...
随机推荐
- 平衡二叉树(AVLTREE,双链表实现)
首先说下好久没更新了,最近打游戏和工作都有点多,o(^▽^)o. 写这个AVL发现自己的代码风格好差,尤其是变量命名这块,后来意识到了,想去改,但是太多了,改了几个就不想改了,做这个是记录下自己的成长 ...
- python基础5(文件操作,with语句)
打开文件 #使用 open f = open('路径',mode = '打开模式', encoding='编码') #可以使用with语句打开,不需要关闭,可以同时打开多个文件 with open(' ...
- 魔兽争霸RPG游戏-军团战争-游戏经验总结
终于要写这篇了,上一篇是个意外. 2015年关注,一代鬼王Xun和GGL比赛.晚上11点之后,经常有水友赛.主播xun,会带着一帮小弟,玩一些游戏.比如魔兽争霸6v6,2v2,RPG游戏-军团战争,疯 ...
- numpy学习笔记 - numpy常用函数、向量化操作及基本数学统计方法
# -*- coding: utf-8 -*-"""主要记录代码,相关说明采用注释形势,供日常总结.查阅使用,不定时更新.Created on Fri Aug 24 19 ...
- enterprise architect (EA) 源码生成UML类图,帮助理解项目工程
用VS看大型工程代码,尤其是很多层类的,很容易头晕,即便是装了visual assist 插件.用VS生成类图吧,只能生成一堆框,只有一些小的类关系有箭头表示.远远不能满足要求.下面介绍建模工具EA来 ...
- Python学习第二天-编写购物车
需求:1.启动程序后,让用户输入工资,然后打印商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 ...
- UVALIVE 4287 Proving Equivalences (强连通分量+缩点)
题意:给定一个图,问至少加入多少条边能够使这个图强连通. 思路:首先求出这个图的强连通分量.然后把每个强连通分量缩成一个点.那么这个图变成了一个DAG,求出全部点的入度和出度,由于强连通图中每个节点的 ...
- poj-1151-Atlantis-线段树求面积并
非常裸的线段树求面积并. 坐标须要离散化一下. #include<stdio.h> #include<iostream> #include<stdlib.h> #i ...
- 启用Database Vault
步骤1:停止EM.监听.数据库 步骤2:启用Database Vault [oracle@single1 ~]$ cd $ORACLE_HOME/rdbms/lib [oracle@single1 l ...
- 一起talk C栗子吧(第九十五回:C语言实例--使用共享内存进行进程间通信一)
各位看官们,大家好,上一回中咱们说的是SystemV IPC结构概述的样例,这一回咱们说的样例是:使用共享内存进行进程间通信. 闲话休提.言归正转.让我们一起talk C栗子吧! 共享内存是Syste ...