forked from powerfun/udpservice
44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
using Newtonsoft.Json;
|
|
using OnlineUserPool.Unility;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace OnlineUserPool.Api
|
|
{
|
|
public class BaseApi
|
|
{
|
|
internal static System.Net.Http.HttpClient httpClient;
|
|
static BaseApi()
|
|
{
|
|
httpClient = new System.Net.Http.HttpClient();
|
|
httpClient.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("OnlineUserPool", "1.0.0"));
|
|
}
|
|
|
|
public static async Task<T> PostAsync<T>(string url, object data)
|
|
{
|
|
StringContent stringContent;
|
|
if (data != null)
|
|
{
|
|
stringContent = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
|
|
}
|
|
else
|
|
{
|
|
stringContent = new StringContent("", Encoding.UTF8);
|
|
}
|
|
var response = await httpClient.PostAsync(ConfigHelp.Host + url, stringContent).ConfigureAwait(false);
|
|
var result = await response.Content.ReadAsStringAsync();
|
|
return JsonConvert.DeserializeObject<T>(result);
|
|
}
|
|
|
|
public static async Task<T> GetAsync<T>(string url)
|
|
{
|
|
var response = await httpClient.GetAsync(ConfigHelp.Host + url, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false);
|
|
var result = await response.Content.ReadAsStringAsync();
|
|
return JsonConvert.DeserializeObject<T>(result);
|
|
}
|
|
}
|
|
}
|