Fork me on GitHub

C#泛型接口请求封装类

using HttpUtil;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

/// <summary>
/// 底层基础方法
/// </summary>
public class BaseClass
{
	private static readonly string _faceIp = "192.168.0.1";   //人脸IP     
	private static readonly string _carIp = "192.168.0.2";   //车行IP
	private static readonly int _port = 80;

	public BaseClass()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    /// <summary>
    /// 停车场查询
    /// </summary>
    /// <param name="plateNo"></param>
    /// <param name="pageSize"></param>
    /// <param name="pageNumber"></param>
    /// <returns></returns>
    public static ParkingLotsInfoDto ParkingLotsInfoFun()
    {
        return CarPost<ParkingLotsInfoDto>("/vms/ParkingLotsInfo", new
        {

        });
    }

    /// <summary>
    /// 人脸
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="url"></param>
    /// <param name="body"></param>
    /// <returns></returns>
    public static T FacePost<T>(string url, dynamic body)
    {
        string bodyStr = JsonConvert.SerializeObject(body);
        return Core<T>(_faceIp+ _port + url, bodyStr, res =>
        {
            if (res.cipher == null) return default(T);
            else return JsonConvert.DeserializeObject<T>(DesEncrypt(res.cipher.ToString()));
        });
    }

    /// <summary>
    /// 车行
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="url"></param>
    /// <param name="body"></param>
    /// <returns></returns>
    public static T CarPost<T>(string url, dynamic body)
    {
        string bodyStr = JsonConvert.SerializeObject(body);
        return Core<T>(_carIp + _port + url, bodyStr, res =>
        {
            if (res.cipher == null) return default(T);
            else return JsonConvert.DeserializeObject<T>(DesEncrypt(res.cipher.ToString()));
        });
    }

    private static T Core<T>(string url, string body, Func<YSResultMsg, T> resFunc)
    {
        try
        {
            var resStr = PostWebRequest(url, body);
            if (resStr.IsNullOrEmpty())
            {
                BaseHelper.WriteLogs("YSAPI", "接口无返回数据请求失败!");
                return default(T);
            }
            YSResultMsg res = JsonConvert.DeserializeObject<YSResultMsg>(resStr);
            T ret = resFunc.Invoke(res);
            return ret;
        }
        catch (Exception ex)
        {
            BaseHelper.WriteLogs("YSAPI_Error", "接口调用失败!" + ex.Message);
            return default(T);
        }
    }

    public static string PostWebRequest(string postUrl, string paramData)
    {
        try
        {
            var requestParams = new { cipher = AesEncrypt(paramData), version="v2.0",timestamp=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") };
            string ret = string.Empty;
            byte[] byteArray = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(requestParams)); //转化
            HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
            webReq.Method = "POST";
            webReq.ContentType = "application/x-www-form-urlencoded";
            webReq.ContentLength = byteArray.Length;
            Stream newStream = webReq.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length);//写入参数
            newStream.Close();
            HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            ret = sr.ReadToEnd();
            sr.Close();
            response.Close();
            newStream.Close();
            return ret;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

    /// <summary>
    /// AES加密
    /// </summary>
    /// <returns></returns>
    protected static string AesEncrypt(string str)
    {
        return str;
    }

    /// <summary>
    /// AES解密
    /// </summary>
    /// <returns></returns>
    protected static string DesEncrypt(string str)
    {
        return str;
    }

    public class YSResultMsg
    {
        public string requestID { get; set; }
        public string code { get; set; }
        public string msg { get; set; }
        public object cipher { get; set; }
    }
}

/// <summary>
/// 停车场功能
/// </summary>
public class ParkingLotsInfoDto
{
    public int Num { get; set; }
    public List<ParkingLotsInfoListDto> ParkingLotsInfoList { get; set; }


    public class ParkingLotsInfoListDto
    {
        public string ParkingLotName { get; set; }
        public string ParkingLotID { get; set; }
        public string TotalParkingNum { get; set; }
        public string RemainParkingNum { get; set; }
        public string RemainOpenParkingNum { get; set; }
    }
}

  

posted @ 2022-11-24 20:14  WantRemake  阅读(56)  评论(0编辑  收藏  举报