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请求 /// /// 公共处理post请求 /// /// 请求参数对象 /// 返回的data对象 /// 主机名 /// 请求地址 /// 请求参数对象 /// 超时时间,单位:秒,默认为30秒 /// 返回对象 public static T1 PostHttpRequest(string host, string url, T param, IDictionary headerDict, bool isNeedWriteRecord = false, int timeout = 30) { try { Guid guid = Guid.NewGuid(); if (isNeedWriteRecord) { HttpRequestRecordHandels.WriteRequestRecord(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(host, url, param, guid, content); } if (!string.IsNullOrEmpty(content)) { T1 result = JsonConvert.DeserializeObject(content); if (result != null) { return result; } } } catch (Exception) { throw; } return default(T1); } //public static Task /// /// 公共处理post请求 /// /// 请求参数对象 /// 返回的data对象 /// 主机名 /// 请求地址 /// 请求参数对象 /// 超时时间,单位:秒,默认为30秒 /// 返回对象 public static ReturnBody PostCommonHttpRequest(string host, string url, T param, IDictionary headerDict, bool isNeedWriteRecord = false, int timeout = 30) { try { Guid guid = Guid.NewGuid(); if (isNeedWriteRecord) { HttpRequestRecordHandels.WriteRequestRecord(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(host, url, param, guid, content); } if (!string.IsNullOrEmpty(content)) { ReturnBody result = JsonConvert.DeserializeObject>(content); if (result != null) { return result; } } } catch (Exception) { throw; } return null; } /// /// 公共处理post请求 /// /// 请求参数对象 /// 返回的data对象 /// 主机名 /// 请求地址 /// 请求参数对象 /// 返回对象 public static ReturnBody PostCommonHttpRequestWithToken(string host, string url, T param, bool isNeedWriteRecord = false, string token = "") { try { Guid guid = Guid.NewGuid(); if (isNeedWriteRecord) { HttpRequestRecordHandels.WriteRequestRecord(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(host, url, param, guid, content); } if (!string.IsNullOrEmpty(content)) { ReturnBody result = JsonConvert.DeserializeObject>(content); if (result != null) { return result; } } } catch (Exception) { throw; } return null; } #endregion #region GET请求 /// /// 公共处理get请求 /// /// 请求参数对象 /// 返回的data对象 /// 主机名 /// 请求地址 /// 请求参数对象 /// 超时时间,单位:秒,默认为30秒 /// 返回对象 public static ReturnBody GetCommonHttpRequest(string host, string url, bool isNeedWriteRecord = false, int timeout = 30) { try { Guid guid = Guid.NewGuid(); if (isNeedWriteRecord) { HttpRequestRecordHandels.WriteRequestRecord(host, url, "", guid); } string content = new HTTPService(host).getContentForString(url, new Guid(), timeout); if (isNeedWriteRecord) { HttpRequestRecordHandels.WriteRequestRecord(host, url, "", guid, content); } if (!string.IsNullOrEmpty(content)) { ReturnBody result = JsonConvert.DeserializeObject>(content); if (result != null) { return result; } } } catch (Exception) { throw; } return null; } /// /// 公共处理get请求 /// /// 返回的data对象 /// 主机名 /// 请求地址 /// 请求参数对象 /// 超时时间,单位:秒,默认为30秒 /// 返回对象 public static T1 GetHttpRequest(string host, string url, bool isNeedWriteRecord = false, int timeout = 30) { try { Guid guid = Guid.NewGuid(); if (isNeedWriteRecord) { HttpRequestRecordHandels.WriteRequestRecord(host, url, "", guid); } string content = new HTTPService(host).getContentForString(url, new Guid(), timeout); if (isNeedWriteRecord) { HttpRequestRecordHandels.WriteRequestRecord(host, url, "", guid, content); } if (!string.IsNullOrEmpty(content)) { T1 result = JsonConvert.DeserializeObject(content); if (result != null) { return result; } } } catch (Exception) { throw; } return default(T1); } #endregion } }