/// <summary>
/// HTTP POST请求
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="value">参数</param>
/// <param name="timeout">超时时间,默认5秒</param>
/// <returns></returns>
public JObject Post(string url, string value, int timeout = 10)
{
JObject result = new JObject();
result["code"] = 99;
result["message"] = "请求失败";
try
{
HttpClient client = new HttpClient();
HttpContent content = new StringContent(value);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
LogHelperUtility.Info(string.Format("[POST]请求参数:{0}", content));
//默认希望响应使用Json序列化(内容协商机制,我接受json格式的数据)
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.Timeout = new TimeSpan(0, 0, timeout);
// 发送请求
var response = client.PostAsync(url, content);
if (response.Result == null || response.Result.StatusCode != System.Net.HttpStatusCode.OK)
{
result["code"] = 1;
result["message"] = "接口请求失败";
return result;
}
// 获取数据
string message = response.Result.Content.ReadAsStringAsync().Result.ToString();
result["code"] = 0;
result["message"] = message;
}
catch (Exception ex)
{
result["code"] = 1;
}
return result;
}