zoukankan      html  css  js  c++  java
  • C#利用using System.Net实现Json数据提交WebAPI

            /// <summary>
            /// 提交Json接口的数据方法
            /// </summary>
            /// <param name="Url"></param>
            /// <param name="jsonParas"></param>
            /// <returns></returns>
            public string Post(string Url, string jsonParas)
            {
                string postContent = "";
                string strURL = Url;
                //创建一个HTTP请求
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
                //Post请求方式
                request.Method = "POST";
                //内容类型
                request.ContentType = "application/json;charset=UTF-8";
                //设置参数,并进行URL编码
                string paraUrlCoded = jsonParas;//System.Web.HttpUtility.UrlEncode(jsonParas);
                byte[] payload;
                //将Json字符串转化为字节
                payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
                //设置请求的ContentLength            
                request.ContentLength = payload.Length;
                //发送请求,获得请求流
                Stream writer;
                try
                {
                    writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
                }
                catch (Exception)
                {
                    writer = null;
                    postContent = "连接服务器失败!";
                }
                //将请求参数写入流
                writer.Write(payload, 0, payload.Length);
                writer.Close();//关闭请求流
                               // String strValue = "";//strValue为http响应所返回的字符流
                HttpWebResponse response;
                try
                {
                    //获得响应流
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (WebException ex)
                {
                    response = ex.Response as HttpWebResponse;
                }
                Stream s = response.GetResponseStream();
                //  Stream postData = Request.InputStream;
                StreamReader sRead = new StreamReader(s);
                postContent = sRead.ReadToEnd();
                sRead.Close();
                return postContent;//返回Json数据
            }

    调用示例

                //写入接口参数准备
                string param = string.Format("tano={0}&taType=1", detail.tano);//签名对象
                string sign = Md5.md5(param, 32).ToUpper();//签名生成,具体根据接口文档
                string urlResult = url + "/tenant/submitDetail?" + param;
                List<SubmitDetailInterface> detailInterfaces = new List<SubmitDetailInterface>();
                SubmitDetailInterface detailInterface = new SubmitDetailInterface()
                {
                    id = detail.id,
                    act = detail.act,
                    supplyBase = detail.supplyBase,
                    amount = detail.amount,
                    baseNo = detail.baseNo,
                    carModel = detail.carModel,
                    partModel = detail.partModel,
                    partName = detail.partName,
                    partNo = detail.partNo,
                    prodBase = detail.prodBase,
                    supplyName = detail.supplyName,
                    supplyNo = detail.supplyNo,
                    taName = detail.taName,
                    tano = detail.tano,
                    taType = detail.taType,
                    updateAt = detail.updateAt,
                    unit = detail.unit
                };
                detailInterfaces.Add(detailInterface);
                string jsonParas = JsonConvert.SerializeObject(new { sign = sign, isIncrement = "1", data = detailInterfaces });
                //写入
                string relust = Post(urlResult, jsonParas);
    CAResult cA
    = JsonConvert.DeserializeObject<CAResult>(relust);//返回结果
        public class CAResult  //返回结果接收类,根据实际情况创建
        {
            public bool success { get; set; }
            public string message { get; set; }
            public string code { get; set; }
            public string result { get; set; }
        }
  • 相关阅读:
    微软外服 AlI In One
    js 循环多次和循环一次的时间的性能对比 All In One
    vue inject All In One
    Excel 表格数据倒置 All In One
    SVG tickets All In One
    OH MY ZSH All In One
    js array for loop performance compare All In One
    mac terminal show You have new mail All In one
    新闻视频 26 制作母版页
    转自牛腩 母版页和相对路径
  • 原文地址:https://www.cnblogs.com/CelonY/p/15538711.html
Copyright © 2011-2022 走看看