C# 将数据推送至其他服务器上

本文介绍了如何在C#中通过`objectdata`和`JsonConvert.SerializeObject`将数据转换为JSON格式,并通过`HttpTool.Post`方法发送到配置的服务器地址。还包含了错误处理和配置管理的部分代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现数据推送源码

用object data接收任意类型,再通过JsonConvert.SerializeObject()转json传给服务器

public string JiudingDataPush(object data, string code)
        {
            try
            {
                string json = JsonConvert.SerializeObject(data);
                //地址配置判断
                string url = ConfigurationManager.AppSettings["JDApi"];
                if (string.IsNullOrEmpty(url)) return "接口地址未配置";
                //传值——地址请求,地址+路径
                string response = new HttpTool().Post(url + "/api/syncData", json );
                //返回结果判断
                if (string.IsNullOrEmpty(response)) return "接口调用失败,返回值为空";
                //json结果实例化
                var responsemodel = FromJson<JuidingRespon>(response);
                //接口判断
                if (responsemodel.code != 1001)
                {
                    return "接口调用失败:" + responsemodel.msg;
                }
                else
                {
                    return "OK";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

属性和构造函数

   #region 属性
    public Encoding encoding { get; set; }
    public string contentType { get; set; }
    #endregion

    #region 构造函数
    public HttpTool()
    {
        if (encoding == null) encoding = System.Text.Encoding.GetEncoding("UTF-8");
        if (string.IsNullOrEmpty(contentType)) contentType = "application/json";
    }
    #endregion

数据请求方法

public string Post(string url, string param)
    {
        //日志记录
        LogHelper.WriteErrorLog($"url:{url},param:{param ?? ""}");
        string result = "";
        Stream outstream = null;
        Stream instream = null;
        StreamReader sr = null;
        HttpWebResponse response = null;
        HttpWebRequest request = null;
        byte[] data = this.encoding.GetBytes(param);
        try
        {
            // 设置参数  
            request = WebRequest.Create(url) as HttpWebRequest;
            CookieContainer cookieContainer = new CookieContainer();
            request.CookieContainer = cookieContainer;
            request.AllowAutoRedirect = true;
            request.Method = "POST";
            request.ContentType = this.contentType;
            request.ContentLength = data.Length;
            outstream = request.GetRequestStream();
            outstream.Write(data, 0, data.Length);
            outstream.Close();
            //发送请求并获取相应回应数据  
            response = request.GetResponse() as HttpWebResponse;
            //直到request.GetResponse()程序才开始向目标网页发送Post请求  
            instream = response.GetResponseStream();
            sr = new StreamReader(instream, encoding);
            //返回结果网页(html)代码 
            return sr.ReadToEnd();
        }
        catch (Exception ex) { 
            result = ex.Message;
            LogHelper.WriteErrorLog($"response:{result}");
        }
        return result;
    }

web.config配置ConfigurationManager.AppSettings[“JDApi”];

value填写 的是地址路径
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

润小仙女

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值