WebAPI请求中的Body Raw参数传递

WebAPI请求中的Body Raw参数传递_第1张图片

利用Fiddle抓取分析:找到工具栏的Any Process监测工具,拖拽到Postman应用程序中,找到该窗体句柄,然后再Postman中执行Send操作,网络请求即被截获到了,如下图所示:

 WebAPI请求中的Body Raw参数传递_第2张图片

 服务器端代码如下:

 [HttpPost]
        [HTTPBasicAuthorize]
        public HttpResponseMessage UpdateAlarmInfo()
        {
            Request.Content.ReadAsStreamAsync().Result.Seek(0, System.IO.SeekOrigin.Begin);
            string content = Request.Content.ReadAsStringAsync().Result;
            try
            {
                var alarm = content.ToObject();
                CacheFactory.Cache().WriteCache(alarm, deviceAlarmViewKey, TimeSpan.FromSeconds(60*2));
                var result = new HttpResponseMessage() { StatusCode = HttpStatusCode.OK };
                return result;
            }
            catch (Exception ex)
            {
                LogFactory.Log.Error("UpdateAlarmInfo Error:", ex);
            }

            return new HttpResponseMessage { Content = new StringContent(new { code = 1 }.ToJson(), Encoding.GetEncoding("UTF-8"), "application/json") };
        }

C# winform 端利用RestSharp调用方法:

 public void UpdateDataView(DcsDataView data)
        {
            try
            {
                var request = new RestSharp.RestRequest("UpdateDataView", RestSharp.Method.POST);
                request.AddObject(data);

                api.Invoke(request, (r) =>
                {
                    if (!r.Status)
                    {
                        LogFactory.Log.WarnFormat(" ...**未成功:{0}", r.Error);
                    }
                });
            }
            catch (Exception ex)
            {
                LogFactory.Log.Error("UpdateDataView 异常:", ex);
            }
        }

HTML Ajax调用方式:

       $.ajax({
                        url: this.$api+'UpdateAlarmInfo',
                        type: 'post',                        
                        dataType: 'json',
                        data:JSON.stringify({ 'HasAlarmInfo':true,'PLCAddress':'M615.1',AlarmMessage:'xxxxxxxx'}),
                        beforeSend: function(request) {                           
                            request.setRequestHeader("Authorization","Basic "+window.btoa("Username:Password"));
                        },
                        success: function (data) {
                            ...
                        },
                        error: function(xhr, errorType, error) {
                            ...
                        }
        });

 

 

你可能感兴趣的:(WebAPI)