139 lines
4.5 KiB
C#
139 lines
4.5 KiB
C#
using Newtonsoft.Json;
|
|
using System.Net;
|
|
using System.Text;
|
|
|
|
namespace YLErp.App
|
|
{
|
|
|
|
/// <summary>
|
|
/// API请求帮助类
|
|
/// </summary>
|
|
static class ApiHelper
|
|
{
|
|
static readonly string BaseAddress = "";
|
|
static readonly HttpClient httpClient;
|
|
|
|
static ApiHelper()
|
|
{
|
|
var handler = new HttpClientHandler
|
|
{
|
|
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
|
|
};
|
|
|
|
httpClient = new HttpClient(handler)
|
|
{
|
|
Timeout = new TimeSpan(0, 3, 0),
|
|
BaseAddress = new Uri(BaseAddress)
|
|
};
|
|
httpClient.DefaultRequestHeaders.Connection.Add("keep-alive");
|
|
httpClient.DefaultRequestHeaders.Add("Keep-Alive", "600");
|
|
}
|
|
|
|
//获取访问令牌
|
|
private static string GetAccessToken()
|
|
{
|
|
using (var request = new HttpRequestMessage()
|
|
{
|
|
Method = HttpMethod.Post,
|
|
RequestUri = new Uri(httpClient.BaseAddress, "api/token"),
|
|
})
|
|
{
|
|
var auth = Convert.ToBase64String(Encoding.UTF8.GetBytes("superA:123321"));
|
|
request.Headers.Add("Authorization", "Basic " + auth);
|
|
using (var response = httpClient.SendAsync(request).Result)
|
|
{
|
|
var res = response.Content.ReadAsStringAsync().Result;
|
|
var model = JsonConvert.DeserializeObject<TokenRsp>(res);
|
|
if (model.errcode > 0)
|
|
{
|
|
throw new Exception(model.errmsg ?? "发生错误");
|
|
}
|
|
return model.accessToken;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 请求API
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="apiPath">api路径</param>
|
|
/// <param name="postData">请求数据</param>
|
|
/// <returns></returns>
|
|
public static async Task<string> GetRequestAsync(string apiPath)
|
|
{
|
|
var accessToken = GetAccessToken();
|
|
|
|
using (var request = new HttpRequestMessage()
|
|
{
|
|
Method = HttpMethod.Get,
|
|
RequestUri = new Uri(httpClient.BaseAddress, apiPath.TrimStart('/')),
|
|
})
|
|
{
|
|
request.Headers.Add("Authorization", "Bearer " + accessToken);
|
|
var response = await httpClient.SendAsync(request);
|
|
return await response.Content.ReadAsStringAsync();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 请求API
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="apiPath">api路径</param>
|
|
/// <param name="postData">请求数据</param>
|
|
/// <returns></returns>
|
|
public static async Task<T> RequestAsync<T>(string apiPath, object postData = null)
|
|
{
|
|
var json = await RequestAsync(apiPath, postData);
|
|
return JsonConvert.DeserializeObject<T>(json);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 请求API
|
|
/// </summary>
|
|
/// <param name="apiPath">api路径</param>
|
|
/// <param name="postData">请求数据</param>
|
|
/// <returns></returns>
|
|
public static async Task<string> RequestAsync(string apiPath, object postData = null)
|
|
{
|
|
var accessToken = GetAccessToken();
|
|
|
|
using (var request = new HttpRequestMessage()
|
|
{
|
|
Method = HttpMethod.Post,
|
|
RequestUri = new Uri(httpClient.BaseAddress, apiPath.TrimStart('/')),
|
|
})
|
|
{
|
|
request.Headers.Add("Authorization", "Bearer " + accessToken);
|
|
if (postData != null)
|
|
{
|
|
var str = JsonConvert.SerializeObject(postData);
|
|
request.Content = new StringContent(str, Encoding.UTF8, "application/json");
|
|
}
|
|
|
|
var response = await httpClient.SendAsync(request);
|
|
return await response.Content.ReadAsStringAsync();
|
|
}
|
|
}
|
|
|
|
class ErrorRsp
|
|
{
|
|
public int errcode { get; set; }
|
|
|
|
public string errmsg { get; set; }
|
|
}
|
|
|
|
class TokenRsp : ErrorRsp
|
|
{
|
|
public string tokenType { get; set; }
|
|
|
|
public string accessToken { get; set; }
|
|
|
|
public int expiresIn { get; set; }
|
|
|
|
public string userName { get; set; }
|
|
}
|
|
}
|
|
}
|