WebService的ASP.NET MVC 3发送和接收 [英] WebService ASP.NET MVC 3 Send and Receive

查看:132
本文介绍了WebService的ASP.NET MVC 3发送和接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经费尽我的大脑几天现在对如何处理的新要求。

I've been racking my brain for a couple of days now on how to approach a new requirement.

我有两个网站。第一个让用户填写一份申请表。第二个网站是一个内部网站,用于管理用户的应用程序。我需要开发从网站1发送应用数据到网站2网络服务,并返回到成功或失败的网站2的响应。我从来没有做过一个Web服务,我在哪里开始有点混乱。我一直在阅读各种在线的例子,但他们似乎都只是为了建立一个web服务的出发点......没有具体的例子。

I have two websites. The first one lets the user fill out an application. The second website is an internal website use to manage the users applications. I need to develop a "web service" that sends the application data from website 1 to website 2 and return a response to website 2 of success or failure. I have never done a web service before and I'm a bit confused on where to start. I've been reading various examples online but they all seem to be just a starting point for building a webservice... no specific examples.

所以,张贴数据网站1,将我的控制器方法是什么样子?我使用JSON的数据发布到网站2?你会和例子是什么样子的?是否有指向的网站2方法某种形式的重定向?

So for posting the data website 1, what would my controller method look like? Do I use Json to post the data to website 2? What would and example of that look like? Is there some form of redirect in the method that points to website 2?

所以张贴响应返回给网站2将是控制器的方法是什么样子?我想我会再次使用JSON来响应发送回网站1?是否有某种形式的方法重定向指回网站1?

So for posting the response back to website 2 what would that controller method look like? I assume I would use Json again to send the response back to website 1? Is there some form of redirect in the method that points back to website 1?

推荐答案

我会使用JSON和POST应用到Web服务。

I would use JSON and POST the application to the web service.

首先我假定应用数据包含在某些类型的对象。使用 JSON.Net 对象序列化到JSON。它看起来像下面这样code。

First I am assuming the application data is contained in some type of object. Use JSON.Net to serialize the object into JSON. It will look something like the following code.

var application = new Application();
string serializedApplication = JsonConvert.Serialize(application);

二是张贴code您的端点(Web服务,MVC动作)。对此,你需要做一个HTT prequest到端点。下面code为我所用,使张贴code。

Second is to POST the code your endpoint(webservice, mvc action). To this you'll need to make a HTTPRequest to the endpoint. The following code is what I use to make to POST the code.

    public bool Post(string url, string body)
    {
        //Make the post
        ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;

        var bytes = Encoding.Default.GetBytes(body);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        Stream stream = null;
        try
        {
            request.KeepAlive = false;
            request.ContentLength = bytes.Length;
            request.ContentType = "application/x-www-form-urlencoded";
            request.Timeout = -1;
            request.Method = "POST";
            stream = request.GetRequestStream();
            stream.Write(bytes, 0, bytes.Length);

        }
        finally
        {
            if (stream != null)
            {
                stream.Flush();
                stream.Close();
            }
        }

        bool success = GetResponse(request);

        return success;
    }

    public bool GetResponse(HttpWebRequest request)
    {
        bool success;

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {

            using (Stream responseStream = response.GetResponseStream())
            {
                if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Created)
                {
                    throw new HttpException((int)response.StatusCode, response.StatusDescription);
                }

                var end = string.Empty;
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    end = reader.ReadToEnd();
                    reader.Close();

                    success = JsonConvert.DeserializeObject<bool>(end);
                }

                response.Close();
            }
        }

        return success;
    }

好,你现在可以发布JSON到端点和接收响应,下一步是创建端点。下面code将让你开始在MVC中的端点将接收一个应用程序并进行处理。

So now you have can POST JSON to an endpoint and receive a response the next step is to create the endpoint. The following code will get you started on an endpoint in mvc that will receive an application and process it.

    [HttpPost]
    public ActionResult SubmitApplication()
    {
        //Retrieve the POSTed payload
         string body;
         using (StreamReader reader = new StreamReader(Request.InputStream))
         {
             body = reader.ReadToEnd();
             reader.Close();
         }

         var application = JsonConvert.Deserialize<Application>(body);

         //Save the application
         bool success = SaveApplication(application);

         //Send the server a response of success or failure.
        return Json(success);
    }

以上code是一个良好的开端。请注意,我没有测试这code。

这篇关于WebService的ASP.NET MVC 3发送和接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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