使用WCF POST JSON
来源:互联网 发布:mysql显示前10行 编辑:程序博客网 时间:2024/06/05 20:34
今天的项目中需要用到WCF POST JSON数据,曾过几番周折,终于成功。记录于此,供有需要的博友采用。
首先数据的写入一定要有一个供公开的类,这与MVC不同。因为之前,自己一直想做为string 传递,就没有打算多加一个数据传递的类。结果WCF总是得不到数据。远程服务器抛出400的错误代码。原因原来是没有定义一个可供序列化的数据传递的类。定义该类以后,数据就能传递到WCF了。
1.类如下:注意要引用 System.Runtime.Serialization;在客户端也要建立相同的类,序列化写入流中。当然也可以不用建类。但是字段必须相同。传入的值为
<pre name="code" class="csharp"> string inputString = "{\"Parameter\":\"ABCDEFG\"}";
[DataContract] public class InputPars { [DataMember] public string Parameter { get; set; } }顺便把客户端传递的代码记录一下.这里需要注意的是地址与MVC调用的方法不同。它的前面一定是.svc文件。后面加上实现了接口的方法名称。
<pre name="code" class="csharp"> public void GetName() { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://localhost/Service.svc/GetData"); request.ContentType = "application/json"; request.Method = "POST"; string inputString = "{\"Key\":\"ABCDEFG\"}"; Byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(inputString); request.ContentLength = byteArray.Length; Stream rstream = request.GetRequestStream(); rstream.Write(byteArray, 0, byteArray.Length); rstream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader SR = new StreamReader(stream); String info = SR.ReadToEnd(); SR.Dispose(); Assert.IsNotNull(info); }
2.有些协议不支持post方法,这里还需要对配置文件 进行设置,例它支持post方法。这里的重点是使用webHttpBinding,和两个行为配置 behaviorConfiguration。注意由于有些终结点是需要不同的协议。因此为单独的终结点配置行为。
<system.serviceModel> <services> <service name="JHDWCFService.Service" behaviorConfiguration="MyServiceBehavior"> <endpoint address="" binding="webHttpBinding" bindingConfiguration="" behaviorConfiguration="webBehavior" name="O2OService" contract="JHDWCF.Contract.Sys.ISysInput" /> </service> </services> <behaviors> <endpointBehaviors > <behavior name="webBehavior" > <webHttp /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name="MyServiceBehavior"> <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false --> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel>
3.至此服务端能够接收json数据了。但是我们希望返回的也是json数据。这个时候需要在Contract中去处理。注意要引用 System.ServiceModel.Web。返回的string对象是采用newtown.json序列化的对象。在这里,如果返回的数据不是string,而是一个对象,比如定义的person,那么程序会自动序列化为json返回。不用自己再去序列化对象了。
[ServiceContract] public interface ISysInput { [OperationContract] [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Method = "POST")] string HttpService(JHDWCF.Model.InputPars pars); }记录完毕。
0 0
- 使用WCF POST JSON
- WCF中REST使用POST方式
- Android通过GET和POST方式访问WCF服务(Json)
- 使用curl发送 POST JSON
- WCF使用小结:(1)WCF接收HTTP POST数据的处理方法
- 使用json往返传输数据 post方法
- python使用post方式发送json参数
- 使用Volley提交Json数据post
- 使用Retrofit通过post提交Json数据
- 使用curl post json到webApi
- java使用https post发送json数据
- 使用fetch post json数据的问题
- httpclient使用post提交json参数
- jquery使用json格式 调用 restful wcf web service
- Android访问WCF服务(使用json实现参数传递)
- Android使用Json和ksoap2调用WebService(WCF)
- Android访问WCF服务(使用json实现参数传递)
- WCF Json应用
- servlet学习笔记
- 师傅又被妖怪抓走了
- Retrofit(重构——图片下载)
- [POJ1001]狼抓兔子 做题笔记
- 求最大公约数的几种方法
- 使用WCF POST JSON
- 标准输出、标准错误重定向(格式)
- bean注入情况下的Junit单元测试
- UITableViewCell 的CATransform3D动画
- openssl 安装
- \Yii::$app 属性整理
- Gson进阶学习
- 你应该知道的 Swift 实用建议
- Vim 使用和技巧