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
|
}
|
}
|