using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
namespace YFAPICommon.Libs
{
public class HttpHelper
{
static public string serverUrl = "http://192.168.0.86:8080/";
public static JObject Post(string url, Dictionary<string, object> param)
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(serverUrl);
//OAuth2令牌
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "xxxx");
//请求超时
client.Timeout = new TimeSpan(5000);
var httpContent = new StringContent(JsonConvert.SerializeObject(param));
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = client.PostAsync(url, httpContent).Result;
var responseValue = response.Content.ReadAsStringAsync().Result;
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return JObject.Parse(responseValue);
}
return null;
}
}
public static JObject Get(string url)
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(serverUrl);
//OAuth2令牌
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "xxxx");
//请求超时
client.Timeout = new TimeSpan(5000);
var response = client.GetAsync(url).Result;
var responseValue = response.Content.ReadAsStringAsync().Result;
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return JObject.Parse(responseValue);
}
return null;
}
}
}
}