测试实体类:(需要在客户端和服务端建了有相同字段名称的实体)

public class CompositeType
{
public CompositeType()
{
SubCompositeTypes = new List<SubCompositeType>() { new SubCompositeType() };
}
public bool BoolValue { get; set; }
public string StringValue { get; set; }
public List<SubCompositeType> SubCompositeTypes
{
get;
set;
}
}
public class SubCompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}

客户端请求代码:

#region JosnPost
CompositeType compositeType = new CompositeType
{
StringValue = "1",
BoolValue = false
}; DataContractJsonSerializer dcSerializer = new DataContractJsonSerializer(typeof(CompositeType)); MemoryStream stream = new MemoryStream(); dcSerializer.WriteObject(stream, compositeType); string data = Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Length); //HttpClient client = new HttpClient(); //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); string link = "http://localhost:1766/Service1.svc/CreateUser"; //HttpResponseMessage respondse = await client.PostAsync(link, new StringContent(data)); //string dataWithJason = await respondse.Content.ReadAsStringAsync(); var request = (HttpWebRequest)WebRequest.Create(new Uri(link));
request.ContentType = "application/json";
request.Method = "POST"; using (var requestStream = await request.GetRequestStreamAsync())
{
var writer = new StreamWriter(requestStream);
writer.Write(data);
writer.Flush();
} using (var resp = await request.GetResponseAsync())
{
using (var responseStream = resp.GetResponseStream())
{
var reader = new StreamReader(responseStream);
var result = reader.ReadToEnd();
}
}

服务端接口定义:

[OperationContract]
[WebInvoke(
UriTemplate = "CreateUser",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
Method = "POST")]
string CreateUser(CompositeType compositeType);

服务端接口实现:

public string CreateUser(CompositeType compositeType)
{
return "OK" + compositeType.StringValue + " " + compositeType.BoolValue + " " + compositeType.SubCompositeTypes.FirstOrDefault().StringValue;
}

由于服务端需要实现REST服务,需要在web.config里重新配置下(cover serviceModel就可以了):

<system.serviceModel>
<services>
<service behaviorConfiguration="MyServiceBehavior" name="Server.Service1">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" contract="Server.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="max" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name ="web">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

经测试,在windows runtime项目中也可以用。

[UWP] - 用Json格式来发送一个Post请求到WCF服务的更多相关文章

  1. angularJS入门小Demo2 【包含不用数据库而用data.json格式响应前台的ajax请求方式测试】

    事件绑定: <html> <head> <title>angularJS入门小demo-5 事件指令</title> <script src=&q ...

  2. requests接口自动化6-Body里json格式数据形式的post请求:json

    Body里json格式数据形式的post请求:用json传参 fiddler里请求响应内容: 传递的json数据 [{"stepId":"0","ca ...

  3. 浏览器何时发送一个Option请求

    Http Options Method 简而言之,OPTIONS请求方法的主要用途有两个: 1.获取服务器支持的HTTP请求方法: 2.用来检查服务器的性能. CORS(跨域资源共享) CORS是一种 ...

  4. charles重复发送一个网络请求&同时发送N次

    重发网络请求的目的:后端调试的过程中,一直在客户端进行点点点比较麻烦,此时直接发送请求比较方便查看调试后的结果(方便后端调试) 1.重复发送一个请求(有两种方法) (1)选定该请求,右键选择repea ...

  5. request模块做post请求时,body为json格式,并且带有中文,如何请求

    后台接口只能解析json,并且一定要是中文才能解析出来,如果是unicode编码的中文则会报错 看requests的源码.以下为解决方法: #将requests库中的models.py文件中的第461 ...

  6. 阶段3 3.SpringMVC·_04.SpringMVC返回值类型及响应数据类型_7 响应json数据之发送ajax的请求

    编写conroller的方法 @RequestBody获取请求体的内容.当前请求体传递的是json字符串. 拿到了请求的内容

  7. X5 内核浏览器对json格式支持的一个小区别

    var json1 = { "data": [{ "type": "pic", "filename": "P6 ...

  8. 如何发送一个http请求—apipost

    API界面功能布局 API请求参数 Header 参数 你可以设置或者导入 Header 参数,cookie也在Header进行设置 Query 参数 Query 支持构造URL参数,同时支持 RES ...

  9. 响应json数据之发送ajax的请求

    一.前端异步请求代码: <span style="font-size:14px;">$.ajax ({ type: "POST", //请求的方式 ...

随机推荐

  1. Java基础教程——Socket编程

    Socket通常也称作"套接字",用于描述IP地址和端口,可以用来实现不同虚拟机或不同计算机之间的通信. --百度百科 套接字允许应用程序插入到网络中,并与插入到网络中的其他应用程 ...

  2. volatile使用

    mq的故障延迟机制:

  3. ubuntu配置网络和静态路由(界面配置形式)

    目录 网卡配置 静态ip配置 静态路由 外网ip配置(动态获取DHCP) 内网ip和静态路由配置 本文主要针对ubuntu18.0系统进行界面形式配置网络.并配置静态路由. 网卡配置 静态ip配置 打 ...

  4. Golang 实现 Redis(8): TCC分布式事务

    本文是使用 golang 实现 redis 系列的第八篇, 将介绍如何在分布式缓存中使用 Try-Commit-Catch 方式来解决分布式一致性问题. godis 集群的源码在Github:Godi ...

  5. 在 Spring Boot 中使用 Flyway

    一.Flyway 介绍 Flyway 是一个开源的数据库迁移工具,MySQL, SQL Server, Oracle 等二十多种数据库 在 Flyway 中数据库的所有改变均称为迁移(migratio ...

  6. Spring Boot 使用 XXL-JOB

    一.配置部署调度中心 1.1 下载源码 https://github.com/xuxueli/xxl-job 1.2 数据库初始化 执行 /xxl-job/doc/db/tables_xxl_job. ...

  7. 文艺splay,占坑等着填

    昨天CF上去就A了前三道题,然后自闭罚坐一个小时什么也没写出来23333.似乎D题人均wa3发就很烦.还是肤浅了 今天精神状态不太好,可能是晚睡的缘故,那不如明天一起写了算了 蹲一波大选结果,蹲一波s ...

  8. 部署 Prometheus 和 Grafana 到 k8s

    在 k8s 中部署 Prometheus 和 Grafana Intro 上次我们主要分享了 asp.net core 集成 prometheus,以及简单的 prometheus 使用,在实际在 k ...

  9. 用java以正确的姿势刷CSP

    许多程序算法考试中,用java是个不错的选择,它几乎实现了所有c++能实现的,所以越来越受Acmer的欢迎.总结一下用到的一些技巧和方法.更多关于csp的可参考海岛blog|皮卡丘 1. 输出 规格化 ...

  10. 第15.6节 PyQt5安装与配置

    一. 引言 关于PyQt5的安装网上有很多的文章,老猿也是学习了好多,最后结合其他模块安装的知识发现其实安装很简单,就是直接使用pip或pip3安装就可以了,这样既无需预先下载好软件,也无需担心版本的 ...