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

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项目中也可以用。

最新文章

  1. 深入理解javascript中实现面向对象编程方法
  2. Gvim插件管理Vundle
  3. Iscroll解析
  4. 20145330《Java程序设计》第五次实验报告
  5. C++ 类
  6. js控制只能输入数字和小数点
  7. jQuery Mobile 基础(第三章)
  8. mysql关于编码部分(乱码出现的原因和解决方法)
  9. 禁用 BootStrap Modal 点击空白时自动关闭
  10. Run busybox httpd with php, sqlite
  11. Android:MD5加密
  12. iframe中调用父iframe中的方法
  13. 关于C#操作INI文件的总结
  14. Java并发编程:线程的基本状态
  15. 【NOIP2016TG】solution
  16. ASP.NET Core Identity Hands On(2)——注册、登录、Claim
  17. CentOS7完成mysql的安装和远程访问
  18. Arduino IDE for ESP8266 项目云盒子(3)外网访问
  19. 【iOS开发】关于显示一连串图片组成动画效果UIImageView的使用
  20. 【LeetCode每天一题】Combination Sum(组合和)

热门文章

  1. odoo13之右上角弹出提示框
  2. DataTable 将一列转为List
  3. 学习abp vnext框架到精简到我的Vop框架
  4. day3(django配置跨域)
  5. 【手把手学习flutter】Flutter打Android包的基本配置和包体积优化策略
  6. HTML5 速览
  7. buu学习记录(上)
  8. 第 4 篇 Scrum 冲刺博客
  9. window下kettle安装
  10. 国际关注,Panda 交易所获悉美银监机构批准特许银行托管加密资产