调用WebAPI接口

   [HttpPost]          接口
public string Post([FromBody] string bjbh)        
{           

          var dwgc = new object();     

           string output = string.Empty;  

           string msg = string.Empty;

            bool result = true;

            string errorCode = "200";

            try            

           {               

                var dwgcbybjbhlist = db.Base_GetDwgcxxByBjbh(bjbh);                

              dwgc = new                    

                 {                      

                 page = pageIndex,      

                 total = dwgcbybjbhlist.Count()%pageSize==0 ? dwgcbybjbhlist.Count()/pageSize : dwgcbybjbhlist.Count()/pageSize + 1,                records = dwgcbybjbhlist.Count(),    

                 dwgcxx =dwgcbybjbhlist//.OrderBy(x => x.bjbh).Skip(pageSize*(pageIndex - 1)).Take(pageSize).ToList(),                     };                }            

           catch (Exception ex)            

            {                

             result = false;    

             msg = ex.Message;           

              errorCode = "300";          

           }   

          finally     

         {                

               var dwgcJson = new                

               {                    

                  Result = result, //执行正常返回true,异常返回false                    

                  ErrorCode = errorCode, //错误编号信息

                    Message = msg, //执行情况说明   

                  Data = dwgc, //合同信息

                };               

             output = Newtonsoft.Json.JsonConvert.SerializeObject(dwgcJson);         

        }            

         return output;      

  }

调用
 private string WebAPITest()
        {
            string postString = "=3333333";
            byte[] postData = System.Text.Encoding.UTF8.GetBytes(postString); //编码,尤其是汉字,事先要看下抓取网页的编码方式 
            var client = new WebClient();
            client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            client.Headers.Add("Accept", "application/json, text/javascript, */*; q=0.01");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可 
            string newUrl = "http://localhost/XMSGXKZService/v1/GetDwgcByBjbh";
            byte[] responseData = client.UploadData(newUrl, "POST", postData); //得到返回字符流 
            string value1 = Encoding.UTF8.GetString(responseData);
            return value1;
        }

 

转载于:https://www.cnblogs.com/hui721/p/4203359.html

你可能感兴趣的:(调用WebAPI接口)