//方法1. Post 异步请求,普通的异步请求,传输普通的字符串等,对于有html代码的字段值的传输支持不好,如果需要传输html,二进制等数据的传输,请使用下面第二个方法,即使用UploadDataAsync
using (var client = new WebClient())
{
var paramJson = ObjectToJson(emailModel);
client.Headers[HttpRequestHeader.ContentType] = "application/json";
cli.UploadStringAsync(new Uri(url), paramJson);
}
//方法2. Post 异步请求,emailModel实体可包含byte[]的字段值
using (var client = new WebClient())
{
var paramJson = ObjectToJson(emailModel);
client.Headers[HttpRequestHeader.ContentType] = "application/json";
var bytes = Encoding.UTF8.GetBytes(paramJson);
client.UploadDataAsync(new Uri(url), "POST", bytes);
}
public static string ObjectToJson(object obj)
{
JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
return jsonSerialize.Serialize(obj);
}
方法3. 使用RestSharp第三方库dll发起Http请求,emailModel实体可包含byte[]的字段值,Rest会自动处理传输数据,进行序列化
var model = new { Name = "NotifyAdminPlaceOrder", Subject = "<%CompanyName%> Cash Exercise行权管理", Body = emailModel.Parameters["@Content"] };
string url2 = "https://localhost:44300/system/GenerateMailTemplate";
var request = new RestRequest();
request.RequestFormat = DataFormat.Json;
request.AddBody(model);
var restClient = new RestClient(url2);
restClient.PostAsync(request, null);