Restful

定义就不多说了,来点干货。网上关于定义有很多。

   public int SendService(string JsonString, string PONo)
        {
            //用于判断是否发送成功
            int Status = 1;
            //获取服务器地址
            string achieveUrl = ConfigurationManager.AppSettings["achieveUrl"].ToString();
            //基于http协议的请求响应   
            HttpWebRequest request = WebRequest.Create(achieveUrl) as HttpWebRequest;  //这里也可以直接用 WebRequest
            //提交方法
            request.Method = "POST"; //基于http的请求方法    HTTP1.0定义了三种请求方法: GET, POST 和 HEAD方法   HTTP1.1新增了五种请求方法:OPTIONS, PUT, DELETE, TRACE 和 CONNECT 方法
            //设置Http标头信息
            request.UserAgent = "";   //根据需求编辑 
            //设置请求超时时间
            request.Timeout = 1000 * 60 * 30;
            //设置读取/写入超时时间
            request.ReadWriteTimeout = 1000 * 60 * 30;

            //request.CookieContainer = new CookieContainer();
            request.ContentType = @"application/json";  //这里的数据格式尤其需要注意 我这里是JSON  
            request.ContentLength = Encoding.UTF8.GetByteCount(JsonString);
            if (!string.IsNullOrEmpty(JsonString))  
            {
                var encoding = new UTF8Encoding();
                var bytes = Encoding.GetEncoding("UTF-8").GetBytes(JsonString);//  
                request.ContentLength = bytes.Length;

                using (var writeStream = request.GetRequestStream())
                {
                    writeStream.Write(bytes, 0, bytes.Length);
                }
            }
            //http请求的返回状态  
            using (var response = (HttpWebResponse)request.GetResponse())  
            {
                //用于返回信息的记录
                var responseValue = string.Empty;

                //判断返回状态  OK代表成功  状态码为200
                if (response.StatusCode != HttpStatusCode.OK)  //有很多种状态 可以允许我们做很多操作
                {
                    //拼接发送失败信息
                    var message = String.Format("Request  HTTP {0}", response.StatusCode);
                    Status = 0;
                }
                else
                {
                    //获取来自 服务器或接口的响应信息
                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream != null)
                            using (var reader = new StreamReader(responseStream))
                            {
                                responseValue = reader.ReadToEnd();  //读取到结尾

                                ReturnResult Data = JsonConvert.DeserializeObject<ReturnResult>(responseValue);
                                #region 把该接口的信息 填到日志中
                                Insert_integrationseverlogs("抛送子单信息到中台", responseValue, Status, achieveUrl, JsonString, null, DateTime.Now, "BTR");
                                #endregion
                                //修改子订单发送时间
                                if (Data.success == "1")
                                {
                                    UpdateSendTime(PONo, DateTime.Now);
                                }
                            }
                    }
                }
            }
            //成功返回 1、 失败返回 0
            return Status;
        }

被调用的接口 我这里的接口是写在MVC中的

 [ActionDescription("订单出库单申请结果")]
        [Public]
        [HttpPost]
        public ActionResult DeliveryOrder()
        {
            
            ReceiveOrderInforVM vm = new ReceiveOrderInforVM();//这里是框架定义

            HttpContext.Response.ContentType = "application/json"; //接收的数据类型
            Stream reqsStream = HttpContext.Request.InputStream;  //接收抛送输出的数据流
            long lSize = reqsStream.Length; //长度
            byte[] byteArray = HttpContext.Request.BinaryRead((int)lSize);
            string strContent = Encoding.ASCII.GetString(byteArray); //编码
            string stringJson = vm.GetInfor(strContent);
            ContentResult result = new ContentResult();
            return Content(stringJson);
        }
原文地址:https://www.cnblogs.com/szlblog/p/7416564.html