HTTP post请求工具类

一、帮助类:

cs 复制代码
using Newtonsoft.Json;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace CommonUtility.Utils
{
    public class JsonContent : StringContent
    {
        public JsonContent(object obj) :
            base(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json")
        { }
    }
    /// <summary>
    /// 基本的http封装
    /// 这里不处理异常 外边处理异常
    /// </summary>
    public static class HttpClientHelper
    {
        //返回model
        public static T PostData<T>(string url, object reposeData) where T : new()
        {

            using (HttpClient client = new HttpClient())
            {
                Task<HttpResponseMessage> response = client.PostAsync(url, new JsonContent(reposeData));
                response.Wait(15000);
                string result = response.Result.Content.ReadAsStringAsync().Result;
                return JsonConvert.DeserializeObject<T>(result);
            }
        }

        //返回string
        public static string PostData(string url)
        {
            using (HttpClient client = new HttpClient())
            {
                Task<HttpResponseMessage> response = client.PostAsync(url, null);
                response.Wait(15000);
                return response.Result.Content.ReadAsStringAsync().Result;
            }

        }

        //返回string
        public static string PostData(string url, object reposeData)
        {

            using (HttpClient client = new HttpClient())
            {
                Task<HttpResponseMessage> response = client.PostAsync(url, new JsonContent(reposeData));
                response.Wait(15000);
                return response.Result.Content.ReadAsStringAsync().Result;
            }

        }
        //返回model
        public static T GetData<T>(string url) where T : new()
        {

            using (HttpClient client = new HttpClient())
            {
                Task<HttpResponseMessage> response = client.GetAsync(url);
                response.Wait(15000);
                string result = response.Result.Content.ReadAsStringAsync().Result;
                return JsonConvert.DeserializeObject<T>(result);
            }

        }
        //返回string
        public static string GetData(string url)
        {
            using (HttpClient client = new HttpClient())
            {
                Task<HttpResponseMessage> response = client.GetAsync(url);
                response.Wait(15000);
                return response.Result.Content.ReadAsStringAsync().Result;
            }

        }
    }
}

二、调用

cs 复制代码
/// <summary>
/// 测试方法1
/// </summary>
[Test]
public void Test1()
{
    string urlgaya = "https://minthtest.gaiaworkforce.com/api/workflow/form/BatchApproveFormsByPriIds/approve";
    string par = "[{\"formNo\":\"3537157\",\"formType\":\"PROCESSLEAVEFORM\",\"approver\":\"BD90B860-7BFC-48CC-8EA9-6EF5BA352CD0\",\"priKey\":\"0de49e45-c712-11ef-90fe-00163e067bfb\",\"tableData\":[{\"approveMessage\":\"\",\"id\":\"0de49e45-c712-11ef-90fe-00163e067bfb\",\"personId\":\"BD90B860-7BFC-48CC-8EA9-6EF5BA352CD0\",\"status\":\"COMPLETED\"}]}]";
    object ss = JsonConvert.DeserializeObject(par);


    string result12 = CommonUtility.Utils.HttpClientHelper.PostData(urlgaya, ss);

    Assert.Pass();
}
cs 复制代码
    QueryResponse result1 = CommonUtility.Utils.HttpClientHelper.PostData<QueryResponse>(url,request);/

三、sqlsurger帮助类

cs 复制代码
using CommDB.Basic;
using CommDB.Exceptions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace CommDB
{
    /// <summary>
    /// 封装ORM
    /// 使用方法基本类似于 SqlSugar ORM 的 SqlSugarClient
    /// 1.封装单表查询、两表连接查询和三表连接查询  
    /// 2.封装常用的重载方式查询 
    /// 3.封装 sql 查询 调用Ado
    /// </summary>
    public class SqlClient
    {
        //这个ado封装了 sqlsugar 的ado 

        private readonly SqlSugarClient db; //私有变量

        public readonly AdoBase Ado; //直接可以使用 执行sql 相关

        public readonly AopProvider Apo;

        #region 初始化函数
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionString">连接字符串</param>
        /// <param name="dbType">数据库类型  "MySql"、 "SqlServer"、 "Sqlite"  "Oracle";</param>
        public SqlClient(string connectionString, DbType dbType = DbType.MySql)
        {
            db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = connectionString, //连接字符串
                DbType = dbType,     // 数据库类型
                IsAutoCloseConnection = true,   //是否自动关闭连接
                //InitKeyType = InitKeyType.SystemTable 
            });
            Ado = new AdoBase(db);
            Apo = db.Aop;
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionString">连接字符串</param>
        /// <param name="dbType">数据库类型  "MySql"、 "SqlServer"、 "Sqlite"  "Oracle";</param>
        /// <param name="isAutoCloseConnection">是否自动关闭数据库</param>
        /// <param name="initKeyType">0使用系统主键 1 使用 属性中定义的主键</param>
        public SqlClient(string connectionString, DbType dbType, bool isAutoCloseConnection = true, int initKeyType = 0)
        {
            ConnectionConfig config = new ConnectionConfig()
            {
                ConnectionString = connectionString, //连接字符串
                DbType = dbType, // 数据库类型
                IsAutoCloseConnection = isAutoCloseConnection, //是否自动关闭连接
            };
            if (initKeyType == 0)
            {
                config.InitKeyType = InitKeyType.SystemTable;
            }
            else if (initKeyType == 1)
            {
                config.InitKeyType = InitKeyType.Attribute;
            }
            db = new SqlSugarClient(config);
            Ado = new AdoBase(db);
        }

        #endregion

        #region 其他变量
        /// <summary>
        /// 临时变量 可以存一些额外的数据 
        /// </summary>
        public Dictionary<string, object> TempItems
        {
            get
            {
                return db.TempItems;
            }
            set
            {
                db.TempItems = value;
            }
        }
        #endregion

        #region 查询

        #region 递归实现查询
        public QueryBase<T> Queryable<T>() where T : class, new()
        {
            return new QueryBase<T>(db).Queryable();
        }
        public QueryBase<T, T2> Queryable<T, T2>(Expression<Func<T, T2, object[]>> joinExpression) where T : class, new()
        {
            return new QueryBase<T, T2>(db).Queryable(joinExpression);
        }
        public QueryBase<T, T2, T3> Queryable<T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression) where T : class, new()
        {
            return new QueryBase<T, T2, T3>(db).Queryable(joinExpression);
        }
        #endregion

        #region 单表查询 重载实现查询

        public List<T> QueryOrderBy<T>(Expression<Func<T, bool>> where = null, Expression<Func<T, object>> orderBy = null, string ascOrDesc = "asc") where T : class, new()
        {
            return Query(@where, orderBy, ascOrDesc);
        }

        public List<T> QueryGroupBy<T>(Expression<Func<T, bool>> where = null, Expression<Func<T, object>> groupBy = null) where T : class, new()
        {
            return Query(@where, null, null, groupBy);
        }

        public List<T> QueryTop<T>(Expression<Func<T, bool>> where = null, int? top = null) where T : class, new()
        {
            return Query(@where, null, null, null, top);
        }

        public List<T> QueryPageList<T>(Expression<Func<T, bool>> where = null, int? pageSize = null, int? pageIndex = null) where T : class, new()
        {
            return Query(@where, null, null, null, null, pageSize, pageIndex);
        }

        public List<T> QueryPageList<T>(Expression<Func<T, bool>> where = null, Expression<Func<T, object>> orderBy = null, string ascOrDesc = "asc", int? pageSize = null, int? pageIndex = null) where T : class, new()
        {
            return Query(@where, orderBy, ascOrDesc, null, null, pageSize, pageIndex);
        }

        public List<T> QueryPageList<T>(Expression<Func<T, bool>> where = null, Expression<Func<T, object>> groupBy = null, int? pageSize = null, int? pageIndex = null) where T : class, new()
        {
            return Query(@where, null, null, groupBy, null, pageSize, pageIndex);
        }

        public List<T> Query<T>(Expression<Func<T, bool>> where = null, Expression<Func<T, object>> orderBy = null, string ascOrDesc = "asc", Expression<Func<T, object>> groupBy = null, int? top = null, int? pageSize = null, int? pageIndex = null) where T : class, new()
        {

            var isQueryable = db.Queryable<T>();
            if (@where != null)
            {
                isQueryable = isQueryable.Where(@where);
            }
            if (orderBy != null)
            {
                if (ascOrDesc.ToLower() == "asc")
                {
                    isQueryable.OrderBy(orderBy, OrderByType.Asc);
                }
                else
                {
                    isQueryable.OrderBy(orderBy, OrderByType.Desc);
                }
            }
            if (groupBy != null)
            {
                isQueryable.GroupBy(groupBy);
            }

            if (top != null)
            {
                isQueryable.Take(top.Value);
            }
            else if (pageIndex != null && pageSize != null)
            {
                isQueryable.ToPageList(pageSize.Value, pageIndex.Value);
            }

            return isQueryable.ToList();

        }



        #endregion

        #region 两个表连接查询 重载实现

        public List<TResult> QueryOrderBy<TResult, T, T2>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, bool>> where = null, Expression<Func<T, T2, object>> orderBy = null, string ascOrDesc = "asc")
            where TResult : class, new()
            where T : class, new()
        {
            return Query<TResult, T, T2>(joinExpression, @where, orderBy, ascOrDesc);
        }

        public List<TResult> QueryGroupBy<TResult, T, T2>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, bool>> where = null, Expression<Func<T, T2, object>> groupBy = null)
            where TResult : class, new()
            where T : class, new()
        {
            return Query<TResult, T, T2>(joinExpression, @where, null, null, groupBy);
        }

        public List<TResult> QueryTop<TResult, T, T2>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, bool>> where = null, int? top = null)
            where TResult : class, new()
            where T : class, new()
        {
            return Query<TResult, T, T2>(joinExpression, @where, null, null, null, top);
        }

        public List<TResult> QueryPageList<TResult, T, T2>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, bool>> where = null, int? pageSize = null, int? pageIndex = null)
            where TResult : class, new()
            where T : class, new()
        {
            return Query<TResult, T, T2>(joinExpression, @where, null, null, null, null, pageSize, pageIndex);
        }

        public List<TResult> QueryPageList<TResult, T, T2>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, bool>> where = null, Expression<Func<T, T2, object>> orderBy = null, string ascOrDesc = "asc", int? pageSize = null, int? pageIndex = null)
            where TResult : class, new()
            where T : class, new()
        {
            return Query<TResult, T, T2>(joinExpression, @where, orderBy, ascOrDesc, null, null, pageSize, pageIndex);
        }

        public List<TResult> QueryPageList<TResult, T, T2>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, bool>> where = null, Expression<Func<T, T2, object>> groupBy = null, int? pageSize = null, int? pageIndex = null)
            where TResult : class, new()
            where T : class, new()
        {
            return Query<TResult, T, T2>(joinExpression, @where, null, null, groupBy, null, pageSize, pageIndex);
        }

        public List<TResult> Query<TResult, T, T2>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, bool>> where = null, Expression<System.Func<T, T2, object>> orderBy = null, string ascOrDesc = "asc", Expression<System.Func<T, T2, object>> groupBy = null, int? top = null, int? pageSize = null, int? pageIndex = null)
            where TResult : class, new()
            where T : class, new()
        {
            if (joinExpression == null)
            {
                throw new CusCommException("连接表达式不能为空");
            }

            var isQueryable = db.Queryable<T, T2>(joinExpression);
            if (@where != null)
            {
                isQueryable = isQueryable.Where(@where);
            }
            if (orderBy != null)
            {
                if (ascOrDesc.ToLower() == "asc")
                {
                    isQueryable.OrderBy(orderBy, OrderByType.Asc);
                }
                else
                {
                    isQueryable.OrderBy(orderBy, OrderByType.Desc);
                }
            }
            if (groupBy != null)
            {
                isQueryable.GroupBy(groupBy);
            }

            if (top != null)
            {
                isQueryable.Take(top.Value);
            }
            else if (pageIndex != null && pageSize != null)
            {
                isQueryable.ToPageList(pageSize.Value, pageIndex.Value);
            }

            return isQueryable.Select((t, t1) => new TResult()).ToList();

        }

        #endregion

        #region 三个表连接查询 重载实现

        public List<TResult> QueryOrderBy<TResult, T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, bool>> where = null, Expression<Func<T, T2, T3, object>> orderBy = null, string ascOrDesc = "asc")
            where TResult : class, new()
            where T : class, new()
        {
            return Query<TResult, T, T2, T3>(joinExpression, where, orderBy, ascOrDesc);
        }

        public List<TResult> QueryGroupBy<TResult, T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, bool>> where = null, Expression<Func<T, T2, T3, object>> groupBy = null)
            where TResult : class, new()
            where T : class, new()
        {
            return Query<TResult, T, T2, T3>(joinExpression, @where, null, null, groupBy);
        }

        public List<TResult> QueryTop<TResult, T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, bool>> where = null, int? top = null)
            where TResult : class, new()
            where T : class, new()
        {
            return Query<TResult, T, T2, T3>(joinExpression, where, null, null, null, top);
        }

        public List<TResult> QueryPageList<TResult, T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, bool>> where = null, int? pageSize = null, int? pageIndex = null)
            where TResult : class, new()
            where T : class, new()
        {
            return Query<TResult, T, T2, T3>(joinExpression, @where, null, null, null, null, pageSize, pageIndex);
        }

        public List<TResult> QueryPageList<TResult, T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, bool>> where = null, Expression<Func<T, T2, T3, object>> orderBy = null, string ascOrDesc = "asc", int? pageSize = null, int? pageIndex = null)
            where TResult : class, new()
            where T : class, new()
        {
            return Query<TResult, T, T2, T3>(joinExpression, @where, orderBy, ascOrDesc, null, null, pageSize, pageIndex);
        }

        public List<TResult> QueryPageList<TResult, T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, bool>> where = null, Expression<Func<T, T2, T3, object>> groupBy = null, int? pageSize = null, int? pageIndex = null)
            where TResult : class, new()
            where T : class, new()
        {
            return Query<TResult, T, T2, T3>(joinExpression, where, null, null, groupBy, null, pageSize, pageIndex);
        }

        public List<TResult> Query<TResult, T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, bool>> where = null, Expression<System.Func<T, T2, T3, object>> orderBy = null, string ascOrDesc = "asc", Expression<System.Func<T, T2, T3, object>> groupBy = null, int? top = null, int? pageSize = null, int? pageIndex = null)
            where TResult : class, new()
            where T : class, new()
        {
            if (joinExpression == null)
            {
                throw new Exception("连接表达式不能为空");
            }

            var isQueryable = db.Queryable<T, T2, T3>(joinExpression);
            if (@where != null)
            {
                isQueryable = isQueryable.Where(@where);
            }
            if (orderBy != null)
            {
                if (ascOrDesc.ToLower() == "asc")
                {
                    isQueryable.OrderBy(orderBy, OrderByType.Asc);
                }
                else
                {
                    isQueryable.OrderBy(orderBy, OrderByType.Desc);
                }
            }
            if (groupBy != null)
            {
                isQueryable.GroupBy(groupBy);
            }

            if (top != null)
            {
                isQueryable.Take(top.Value);
            }
            else if (pageIndex != null && pageSize != null)
            {
                isQueryable.ToPageList(pageSize.Value, pageIndex.Value);
            }

            return isQueryable.Select((t, t1) => new TResult()).ToList();

        }

        #endregion

        #endregion

        #region 插入


        #region 直接调用
        public virtual int Insertable<T>(List<T> insertObjs) where T : class, new()
        {
            if (insertObjs == null || insertObjs.Count == 0)
            {
                return 0;
            }

            Utity<T>.RemoveSpace(insertObjs);

            return db.Insertable<T>(insertObjs).ExecuteCommand();
        }

        public virtual int Insertable<T>(T insertObj) where T : class, new()
        {
            Utity<T>.RemoveSpace(insertObj);
            return this.Insertable(new List<T>() { insertObj });
        }

        public virtual T InsertableReturnEntity<T>(T insertObj) where T : class, new()
        {
            Utity<T>.RemoveSpace(insertObj);
            return db.Insertable<T>(insertObj).ExecuteReturnEntity();
        }

        //插入不同类型的 对象  对象必须 可以 where T : class, new()
        public virtual int Insertable(List<dynamic> insertObjs)
        {
            if (insertObjs == null || insertObjs.Count == 0)
            {
                return 0;
            }
            try
            {
                db.Ado.BeginTran();
                foreach (var insertObj in insertObjs)
                {
                    db.Insertable(insertObj).ExecuteCommand();
                }
                db.Ado.CommitTran();
                return insertObjs.Count;
            }
            catch (Exception)
            {
                db.Ado.RollbackTran();

                return 0;
            }
        }
        #endregion


        #region 递归调用
        public virtual InsertBase<T> InsertableBase<T>(List<T> insertObjs) where T : class, new()
        {
            return new InsertBase<T>(db).Insertable(insertObjs);
        }

        public virtual InsertBase<T> InsertableBase<T>(T insertObj) where T : class, new()
        {
            return new InsertBase<T>(db).Insertable(insertObj);
        }
        public virtual InsertBase<T> InsertableBase<T>(dynamic insertDynamicObject) where T : class, new()
        {
            return new InsertBase<T>(db).Insertable(insertDynamicObject);
        }
        #endregion

        #endregion

        #region 更新
        #region 直接调用
        public virtual int Updateable<T>(List<T> UpdateObjs) where T : class, new()
        {
            if (UpdateObjs == null || UpdateObjs.Count == 0)
            {
                return 0;
            }
            Utity<T>.RemoveSpace(UpdateObjs);

            return db.Updateable<T>(UpdateObjs).Where(true).ExecuteCommand();
        }

        /// <summary>
        /// 更新单个字段
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="UpdataObj"></param>
        /// <param name="columns"></param>
        public virtual void UpdateColumns<T>(T UpdataObj, Expression<Func<T, object>> columns) where T : class, new()
        {
            Utity<T>.RemoveSpace(UpdataObj);
            db.Updateable(UpdataObj).UpdateColumns(columns).ExecuteCommand();
        }
        public virtual int Updateable<T>(T UpdateObj) where T : class, new()
        {
            Utity<T>.RemoveSpace(UpdateObj);
            return this.Updateable(new List<T>() { UpdateObj });
        }

        //更新不同类型的 对象  对象必须 可以 where T : class, new()
        public virtual int Updateable(List<dynamic> UpdateObjs)
        {
            if (UpdateObjs == null || UpdateObjs.Count == 0)
            {
                return 0;
            }
            try
            {
                db.Ado.BeginTran();
                foreach (var updateObj in UpdateObjs)
                {
                    db.Updateable(updateObj).ExecuteCommand();
                }
                db.Ado.CommitTran();
                return UpdateObjs.Count;
            }
            catch (Exception)
            {
                db.Ado.RollbackTran();
                return 0;
            }
        }
        #endregion

        #region 递归调用
        public virtual UpdateBase<T> UpdateableBase<T>() where T : class, new()
        {
            return new UpdateBase<T>(db).Updateable();
        }
        public virtual UpdateBase<T> UpdateableBase<T>(List<T> UpdateObjs) where T : class, new()
        {
            return new UpdateBase<T>(db).Updateable(UpdateObjs);
        }

        public virtual UpdateBase<T> UpdateableBase<T>(T[] UpdateObjs) where T : class, new()
        {
            return new UpdateBase<T>(db).Updateable(UpdateObjs);
        }
        public virtual UpdateBase<T> UpdateableBase<T>(T UpdateObj) where T : class, new()
        {
            return new UpdateBase<T>(db).Updateable(UpdateObj);
        }
        #endregion
        #endregion

        #region 删除
        #region 直接调用
        private IDeleteable<T> Deleteable<T>() where T : class, new()
        {
            return db.Deleteable<T>();
        }

        public virtual int Deleteable<T>(Expression<Func<T, bool>> expression) where T : class, new()
        {
            return this.Deleteable<T>().Where(expression).ExecuteCommand();
        }

        public virtual int Deleteable<T>(T deleteObj) where T : class, new()
        {
            return this.Deleteable<T>().Where(deleteObj).ExecuteCommand();
        }

        public virtual int Deleteable<T>(List<T> deleteObjs) where T : class, new()
        {
            return this.Deleteable<T>().Where(deleteObjs).ExecuteCommand();
        }

        //删除不同类型的 对象  对象必须 可以 where T : class, new()
        public virtual int Deleteable(List<dynamic> deleteObjs)
        {
            if (deleteObjs == null || deleteObjs.Count == 0)
            {
                return 0;
            }
            try
            {
                db.Ado.BeginTran();
                foreach (var deleteObj in deleteObjs)
                {
                    db.Deleteable(deleteObj).ExecuteCommand();
                }
                db.Ado.CommitTran();
                return deleteObjs.Count;
            }
            catch (Exception)
            {
                db.Ado.RollbackTran();
                return 0;
            }
        }
        #endregion

        #region 递归调用
        public virtual DeleteBase<T> DeleteableBase<T>() where T : class, new()
        {
            return new DeleteBase<T>(db).Deleteable();
        }

        public virtual DeleteBase<T> DeleteableBase<T>(Expression<Func<T, bool>> expression) where T : class, new()
        {
            return new DeleteBase<T>(db).Deleteable(expression);
        }
        public virtual DeleteBase<T> DeleteableBase<T>(T deleteObj) where T : class, new()
        {
            return new DeleteBase<T>(db).Deleteable(deleteObj);
        }

        public DeleteBase<T> DeleteableBase<T>(List<T> deleteObjs) where T : class, new()
        {
            return new DeleteBase<T>(db).Deleteable(deleteObjs);
        }
        #endregion

        #endregion

    }
}
相关推荐
幽兰的天空4 小时前
介绍 HTTP 请求如何实现跨域
网络·网络协议·http
心平气和️4 小时前
HTTP 配置与应用(不同网段)
网络·网络协议·计算机网络·http
心平气和️4 小时前
HTTP 配置与应用(局域网)
网络·计算机网络·http·智能路由器
喜-喜4 小时前
C# HTTP/HTTPS 请求测试小工具
开发语言·http·c#
Gworg4 小时前
网站HTTP改成HTTPS
网络协议·http·https
Mbblovey5 小时前
Picsart美易照片编辑器和视频编辑器
网络·windows·软件构建·需求分析·软件需求
北顾南栀倾寒5 小时前
[Qt]系统相关-网络编程-TCP、UDP、HTTP协议
开发语言·网络·c++·qt·tcp/ip·http·udp
GZ_TOGOGO6 小时前
PIM原理与配置
网络·华为·智能路由器
7ACE6 小时前
Wireshark TS | 虚假的 TCP Spurious Retransmission
网络·网络协议·tcp/ip·wireshark·tcpdump