Fork me on GitHub

C#泛型接口请求封装类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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 @   WantRemake  阅读(59)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示