59 lines
2.0 KiB
C#
59 lines
2.0 KiB
C#
|
using HL_FristAidPlatform_DTO;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Configuration;
|
|||
|
using System.IO;
|
|||
|
using System.Net.Http;
|
|||
|
using System.Runtime.Serialization.Json;
|
|||
|
|
|||
|
namespace HL_FristAidPlatform_Public
|
|||
|
{
|
|||
|
public static class HttpClientHelp<T>
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 接口地址
|
|||
|
/// </summary>
|
|||
|
public static string WebApiUrl = ConfigurationManager.AppSettings["WebApiUrl"];
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Post 请求
|
|||
|
/// </summary>
|
|||
|
/// <param name="Url"></param>
|
|||
|
/// <param name="t"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static ListEntity<T> Post(string Url, List<T> t)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
//创建一个处理序列化的DataContractJsonSerializer
|
|||
|
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
|
|||
|
MemoryStream ms = new MemoryStream();
|
|||
|
|
|||
|
//将资料写入MemoryStream
|
|||
|
serializer.WriteObject(ms, t[0]);
|
|||
|
|
|||
|
//一定要在这设定Position
|
|||
|
ms.Position = 0;
|
|||
|
string url = WebApiUrl + Url;
|
|||
|
|
|||
|
//将MemoryStream转成HttpContent
|
|||
|
HttpContent content = new StreamContent(ms);
|
|||
|
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
|
|||
|
|
|||
|
ListEntity<T> returnT = new ListEntity<T>();
|
|||
|
HttpClient client = new HttpClient();
|
|||
|
//由HttpClient发出Post Method
|
|||
|
HttpResponseMessage response = client.PostAsync(url, content).Result;
|
|||
|
returnT.Success = response.IsSuccessStatusCode;
|
|||
|
returnT.Msg = response.RequestMessage.ToString();
|
|||
|
|
|||
|
return returnT;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
PublicClass.WriteErrorLog("HttpClientHelp", "Post:\r\n" + ex);
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|