如何将对象发送到WCF REST Web服务 [英] How to send a Object to WCF REST web service

查看:94
本文介绍了如何将对象发送到WCF REST Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 WCF REST Web服务的新手
我已经写了一个基于休息的Web服务来抱怨.一些属性是ComplainType,ComplainBody,Image等.
因此,我要通过 android 网站调用此服务,并且参数应为投诉对象类型.

那么怎么做呢? ?
我受够了搜索:(
谢谢

I''m new to WCF REST Web Services
I have write a rest based web service for complaining. Some attributes are ComplainType, ComplainBody, Image etc.
So what i want is to call this service via a android and from website and parameter should be Complain Object type.

So what is the way to do this. ?
I''m Fed up of searching this :(
thank you

推荐答案

好.最终,我与朋友交谈后,自己找到了解决方案. :D.因此,让我们看一下如何通过WCF REST Web服务发送复杂类型.

因此,让我们考虑一下一个包含MakeComplain的Interface类,该方法将一个抱怨对象作为参数.并返回一个布尔值以确保该对象已接收.
Ok. Finally I found a solution by myself after talking with my friend. :D. So let''s look at how to send a Complex type over WCF REST Web Service.

So let''s think that there is a Interface class which contain MakeComplain and this method take a complain object as a parameter. and it''s return a boolean value to ensure that the object received.
[ServiceContract]
    public interface IComplainService
    {
        [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat=      WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]
        bool MakeCompalin(Complain comp);
    } 



因此,让我们看一下Interface类的实现.在听到消息时,我们检查对象是否已收到.



So, Let''s look at the Implementation of the Interface class. In hear we check whether the object is received.

public class ComplainService : IComplainService
    {
        public bool MakeCompalin(Complain comp)
        {
            if (comp != null)
            {                               
                return true;
            }
            else
            {
                return false;
            } 
        }
    }



我希望每个人都知道如何在web.config文件中进行配置.我想我不想提它听到的话. ;)

现在让我们创建一个客户项目来测试服务.我已经创建了一个控制台应用程序来测试服务.



I hope everyone knows how to do the configuration part in web.config file. I think i don''t want to mention it hear. ;)

Now Let''s create a Client Project to test the Service. I have created a console application to test the service.

class Program
    {
        static void Main(string[] args)
        {
            Complain complain = new Complain()
            {
                CompainType = "type1",
                CompainBody = "Body1"
            };
            
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string output = serializer.Serialize(complain);

            string strUri = "http://localhost:6595/ComplainService.svc/MakeCompalin";
            Uri uri = new Uri(strUri);
            WebRequest request = WebRequest.Create(uri);
            request.Method = "POST";
            request.ContentType = "application/json; charset=utf-8";

            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            string serOut = jsonSerializer.Serialize(complain);

            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(serOut);
            }

            WebResponse responce = request.GetResponse();
            Stream reader = responce.GetResponseStream();

            StreamReader sReader = new StreamReader(reader);
            string outResult = sReader.ReadToEnd();
            sReader.Close();
        }



听到我在做什么是通过POST方法与服务进行通信.首先,我必须使用JavaScriptSerializer序列化为jason消息,并将其写入使用StreamReader创建的writer中.我希望你们知道其余的事情..;)
这就是所有的:D

这可能不是执行此操作的理想方法.如果还有其他方法.请与我们分享...;)



In hear what i''m doing is communicate to service via POST method. First i have to serialize in to jason message using JavaScriptSerializer and write that into writer which is created using StreamReader. I hope rest of the things u guys know.. ;)
So that''s all :D

this may not be the ideal method to do this. If there are any other methods. Please do share with us... ;)


这篇关于如何将对象发送到WCF REST Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆