zs
2024-11-21 aee9f67e702f6467aa1948777251fa1bf85353ec
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace DataCapture_MA.HttpHelper
{
    public class HttpRequesHelper
    {
        #region POST请求
 
        /// <summary>
        /// 公共处理post请求
        /// </summary>
        /// <typeparam name="T">请求参数对象</typeparam>
        /// <typeparam name="T1">返回的data对象</typeparam>
        /// <param name="host">主机名</param>
        /// <param name="url">请求地址</param>
        /// <param name="param">请求参数对象</param>
        ///  <param name="timeout">超时时间,单位:秒,默认为30秒</param>
        /// <returns>返回对象</returns>
        public static T1 PostHttpRequest<T, T1>(string host, string url, T param, IDictionary<string, string> headerDict, bool isNeedWriteRecord = false, int timeout = 30)
        {
            try
            {
                Guid guid = Guid.NewGuid();
                if (isNeedWriteRecord)
                {
                    HttpRequestRecordHandels.WriteRequestRecord<T>(host, url, param, guid);
                }
 
 
                JavaScriptDateTimeConverter convert = new JavaScriptDateTimeConverter();
                var strParam = JsonConvert.SerializeObject(param, Formatting.None, convert);//序列化后的字符串
                string content = new HTTPService(host).postContentForString(url,
                                strParam, headerDict, timeout);
                if (isNeedWriteRecord)
                {
                    HttpRequestRecordHandels.WriteRequestRecord<T>(host, url, param, guid, content);
                }
                if (!string.IsNullOrEmpty(content))
                {
                    T1 result = JsonConvert.DeserializeObject<T1>(content);
                    if (result != null)
                    {
                        return result;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return default(T1);
        }
 
        //public static Task 
        /// <summary>
        /// 公共处理post请求
        /// </summary>
        /// <typeparam name="T">请求参数对象</typeparam>
        /// <typeparam name="T1">返回的data对象</typeparam>
        /// <param name="host">主机名</param>
        /// <param name="url">请求地址</param>
        /// <param name="param">请求参数对象</param>
        ///  <param name="timeout">超时时间,单位:秒,默认为30秒</param>
        /// <returns>返回对象</returns>
        public static ReturnBody<T1> PostCommonHttpRequest<T, T1>(string host, string url, T param, IDictionary<string, string> headerDict, bool isNeedWriteRecord = false, int timeout = 30)
        {
            try
            {
                Guid guid = Guid.NewGuid();
                if (isNeedWriteRecord)
                {
                    HttpRequestRecordHandels.WriteRequestRecord<T>(host, url, param, guid);
                }
 
                JavaScriptDateTimeConverter convert = new JavaScriptDateTimeConverter();
                var strParam = JsonConvert.SerializeObject(param, Formatting.None, convert);//序列化后的字符串
                string content = new HTTPService(host).postContentForString(url,
                                strParam, headerDict, timeout);
                if (isNeedWriteRecord)
                {
                    HttpRequestRecordHandels.WriteRequestRecord<T>(host, url, param, guid, content);
                }
                if (!string.IsNullOrEmpty(content))
                {
                    ReturnBody<T1> result = JsonConvert.DeserializeObject<ReturnBody<T1>>(content);
                    if (result != null)
                    {
                        return result;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return null;
        }
 
 
        /// <summary>
        /// 公共处理post请求
        /// </summary>
        /// <typeparam name="T">请求参数对象</typeparam>
        /// <typeparam name="T1">返回的data对象</typeparam>
        /// <param name="host">主机名</param>
        /// <param name="url">请求地址</param>
        /// <param name="param">请求参数对象</param>
        /// <returns>返回对象</returns>
        public static ReturnBody<T1> PostCommonHttpRequestWithToken<T, T1>(string host, string url, T param, bool isNeedWriteRecord = false, string token = "")
        {
            try
            {
 
                Guid guid = Guid.NewGuid();
                if (isNeedWriteRecord)
                {
                    HttpRequestRecordHandels.WriteRequestRecord<T>(host, url, param, guid);
                }
 
                JavaScriptDateTimeConverter convert = new JavaScriptDateTimeConverter();
                var strParam = JsonConvert.SerializeObject(param, Formatting.None, convert);//序列化后的字符串
                string content = new HTTPService(host).postContentForStringWithToken(url,
                                strParam, new Guid(), token);
                if (isNeedWriteRecord)
                {
                    HttpRequestRecordHandels.WriteRequestRecord<T>(host, url, param, guid, content);
                }
                if (!string.IsNullOrEmpty(content))
                {
                    ReturnBody<T1> result = JsonConvert.DeserializeObject<ReturnBody<T1>>(content);
                    if (result != null)
                    {
                        return result;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return null;
        }
 
 
        #endregion
 
        #region GET请求
 
        /// <summary>
        /// 公共处理get请求
        /// </summary>
        /// <typeparam name="T">请求参数对象</typeparam>
        /// <typeparam name="T1">返回的data对象</typeparam>
        /// <param name="host">主机名</param>
        /// <param name="url">请求地址</param>
        /// <param name="param">请求参数对象</param>
        /// <param name="timeout">超时时间,单位:秒,默认为30秒</param>
        /// <returns>返回对象</returns>
        public static ReturnBody<T1> GetCommonHttpRequest<T1>(string host, string url, bool isNeedWriteRecord = false, int timeout = 30)
        {
            try
            {
                Guid guid = Guid.NewGuid();
                if (isNeedWriteRecord)
                {
                    HttpRequestRecordHandels.WriteRequestRecord<String>(host, url, "", guid);
                }
 
                string content = new HTTPService(host).getContentForString(url, new Guid(), timeout);
                if (isNeedWriteRecord)
                {
                    HttpRequestRecordHandels.WriteRequestRecord<String>(host, url, "", guid, content);
                }
                if (!string.IsNullOrEmpty(content))
                {
                    ReturnBody<T1> result = JsonConvert.DeserializeObject<ReturnBody<T1>>(content);
                    if (result != null)
                    {
                        return result;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return null;
        }
 
 
        /// <summary>
        /// 公共处理get请求
        /// </summary>
        /// <typeparam name="T1">返回的data对象</typeparam>
        /// <param name="host">主机名</param>
        /// <param name="url">请求地址</param>
        /// <param name="param">请求参数对象</param>
        /// <param name="timeout">超时时间,单位:秒,默认为30秒</param>
        /// <returns>返回对象</returns>
        public static T1 GetHttpRequest<T1>(string host, string url, bool isNeedWriteRecord = false, int timeout = 30)
        {
            try
            {
                Guid guid = Guid.NewGuid();
                if (isNeedWriteRecord)
                {
                    HttpRequestRecordHandels.WriteRequestRecord<String>(host, url, "", guid);
                }
                string content = new HTTPService(host).getContentForString(url, new Guid(), timeout);
                if (isNeedWriteRecord)
                {
                    HttpRequestRecordHandels.WriteRequestRecord<String>(host, url, "", guid, content);
                }
                if (!string.IsNullOrEmpty(content))
                {
                    T1 result = JsonConvert.DeserializeObject<T1>(content);
                    if (result != null)
                    {
                        return result;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return default(T1);
        }
 
        #endregion
    }
}