[UWP] - 用Json格式来发送一个Post请求到WCF服务
2024-09-05 04:24:12
测试实体类:(需要在客户端和服务端建了有相同字段名称的实体)
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项目中也可以用。
最新文章
- 深入理解javascript中实现面向对象编程方法
- Gvim插件管理Vundle
- Iscroll解析
- 20145330《Java程序设计》第五次实验报告
- C++ 类
- js控制只能输入数字和小数点
- jQuery Mobile 基础(第三章)
- mysql关于编码部分(乱码出现的原因和解决方法)
- 禁用 BootStrap Modal 点击空白时自动关闭
- Run busybox httpd with php, sqlite
- Android:MD5加密
- iframe中调用父iframe中的方法
- 关于C#操作INI文件的总结
- Java并发编程:线程的基本状态
- 【NOIP2016TG】solution
- ASP.NET Core Identity Hands On(2)——注册、登录、Claim
- CentOS7完成mysql的安装和远程访问
- Arduino IDE for ESP8266 项目云盒子(3)外网访问
- 【iOS开发】关于显示一连串图片组成动画效果UIImageView的使用
- 【LeetCode每天一题】Combination Sum(组合和)