WebService服务模拟HttpPost、HttpGet调用

web.asmx页面写webService服务的方法

   /// <summary>
        /// 插入
        /// </summary>
        /// <param name="list"></param>
        [WebMethod]
        public void InsertDealerCheckInfo(string strJson)//List<DealerCheckInfo> list
        {
            bool value = false;
            apiBLL.TrTableDealerCheck();
            WebResponse result = new WebResponse();
            List<DealerCheckInfo> list = JsonConvert.DeserializeObject<List<DealerCheckInfo>>(strJson);
            foreach (DealerCheckInfo dkInfo in list)
            {
                int iIsSubcontract = dkInfo.IsSubcontract ? 1 : 0;
                int iIsAuthor = dkInfo.IsAuthor ? 1 : 0;
                value = apiBLL.InsertDealerCheckInfo(dkInfo, iIsSubcontract, iIsAuthor);
            }
            if (value)
            {
                result.code = 200;
                result.message = "回写成功";
            }
            else
            {
                result.code = 500;
                result.message = "回写失败";
            }
            string json = JsonConvert.SerializeObject(result);
            Context.Response.ContentType = "json";
            Context.Response.Write(json);
        }
        /// <summary>
        ///  查询
        /// </summary>
        [WebMethod]
        public void GetBusyworkDailyList()
        {
            List<BusyworkDailyInfo> list = apiBLL.GetBusyworkDailyList();
            string json = JsonConvert.SerializeObject(list);
            Context.Response.ContentType = "json";
            Context.Response.Write(json);
        }
        /// <summary>
        /// 查询
        /// </summary>
        [WebMethod]
        public void GetBusyworkSignInList()
        {
            List<BusyworkSignInInfo> list = apiBLL.GetBusyworkSignInList();
            string json = JsonConvert.SerializeObject(list);
            Context.Response.ContentType = "json";
            Context.Response.Write(json);
        }

模拟调用方法

  /// <summary>
        ///  Post请求调用WebService,将参数序列化传递
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="method">方法</param>
        /// <param name="list">参数</param>
        /// <returns></returns>
        private string HttpPostWebService(string url, string method, List<DealerCheckInfo> list)
        {
            string param = HttpUtility.UrlEncode("strJson") + "=" + HttpUtility.UrlEncode(JsonConvert.SerializeObject(list));
            char[] chars = Encoding.UTF8.GetChars(Encoding.UTF8.GetBytes(param));
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "/" + method);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = chars.Length;
            StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
            writer.Write(chars, 0, chars.Length);
            writer.Flush();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(myResponseStream, Encoding.UTF8);
            string retString = reader.ReadToEnd();
            reader.Close();
            myResponseStream.Close();
            return retString;
        }
        /// <summary>
        /// Get请求调用WebService
        /// </summary>
        /// <param name="Url">地址</param>
        /// <param name="method">方法</param>
        /// <returns></returns>
        private string HttpGetWebService(string url, string method)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "/" + method);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
        }

页面调用

///HttpGet调用,将返回的流反序列化

 List<BusyworkDailyInfo> dailyInfos = JsonConvert.DeserializeObject<List<BusyworkDailyInfo>>(HttpGetWebService(webServiceUrl, "GetBusyworkDailyList"));

List<BusyworkSignInInfo> signInInfos = JsonConvert.DeserializeObject<List<BusyworkSignInInfo>>(HttpGetWebService(webServiceUrl, "GetBusyworkSignInList"));

/模拟post请求调用Web服务
  string result = HttpPostWebService(webServiceUrl, "InsertDealerCheckInfo", listDealerCheck);
  if(result.Contains("500"))
    zero_flag = false;
  else
    zero_flag = true;  }

总结:
1、WebService中的方法无论返回什么结果(string list等)都要将结果序列化传递
2、模拟HttpGet调用时,将结果(string list等)反序列化拿到并赋值
3、模拟HttpPost调用时,将参数(stirng list等)序列化传递,那这时传递的参数实际上就是string 类型
以上是自己做的时候的总结, 不代表所有,可能也有不足之处
关于[序列化和反序列化],感觉这篇讲的很详细:
https://www.cnblogs.com/fengxuehuanlin/p/5700282.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值