public class Response<T>
{
public bool IsSuccess { get; set; }
public string Message { get; set; }
public T Ins { get; set; }
}
public class Response
{
public int status { get; set; }
public bool success { get; set; }
public string msg { get; set; }
}
public class HttpService
{
HttpClient _Client;
string IP;
string BaseUri;
public HttpService(HttpClient httpClient)
{
_Client = httpClient;
_Client.DefaultRequestHeaders.Accept.Clear();
_Client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
_Client.Timeout = new TimeSpan(0, 0, 10);
BaseUri = $"http://192.168.1.10:9000/api/";
}
public void ChangeIp(string ip)
{
IP = ip;
//IP = "192.168.1.13";
BaseUri = $"http://{IP}:9000/api/";
}
JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions()
{
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};
public async Task<Response<T>> GetAsync<T>(string uri)
{
string full_uri = BaseUri+uri;
Response<T> response = new Response<T>();
response.IsSuccess = true;
try
{
var responseM = await _Client.GetAsync(full_uri);
responseM.EnsureSuccessStatusCode();
string responseBody = await responseM.Content.ReadAsStringAsync();
response.Ins = JsonSerializer.Deserialize<T>(responseBody);
}
catch (Exception ex)
{
response.IsSuccess = false;
response.Message = ex.Message;
}
return response;
}
public async Task<Response<T>> DeleteAsync<T>(string uri)
{
string full_uri = BaseUri + uri;
Response<T> response = new Response<T>();
response.IsSuccess = true;
try
{
var responseM = await _Client.DeleteAsync(full_uri);
responseM.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
response.IsSuccess = false;
response.Message = ex.Message;
}
return response;
}
public async Task<Response<T>> PutAsync<T>(string uri,T ins)
{
string full_uri = BaseUri + uri;
Response<T> response = new Response<T>();
response.IsSuccess = true;
var json=JsonSerializer.Serialize(ins, jsonSerializerOptions);
var content = new StringContent(json, Encoding.UTF8, "application/json");
string msg = string.Empty;
try
{
var responseM = await _Client.PutAsync(full_uri, content);
msg = await responseM.Content.ReadAsStringAsync();
responseM.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
response.IsSuccess = false;
response.Message = ex.Message+Environment.NewLine+ msg;
//App.NotificationContainer.AddNotifiaction(new NotificationItem("更新失败", response.Message, NotifiactionType.Error));
}
return response;
}
public async Task<Response> PostAsync<T>(string uri, T ins)
{
string full_uri = BaseUri + uri;
Response response = new();
var json = JsonSerializer.Serialize(ins, jsonSerializerOptions);
var content = new StringContent(json, Encoding.UTF8, "application/json");
string msg = string.Empty;
try
{
var responseM = await _Client.PostAsync(full_uri, content);
msg = await responseM.Content.ReadAsStringAsync();
responseM.EnsureSuccessStatusCode();
string responseBody = await responseM.Content.ReadAsStringAsync();
response = JsonSerializer.Deserialize<Response>(responseBody);
}
catch (Exception ex)
{
response.success = false;
response.msg = ex.Message+Environment.NewLine+msg;
//App.NotificationContainer.AddNotifiaction(new NotificationItem("添加失败", response.Message, NotifiactionType.Error));
}
return response;
}
public async Task<Response<T1>> PostAsync<T,T1>(string uri, T ins)
{
string full_uri = BaseUri + uri;
Response<T1> response = new();
var json = JsonSerializer.Serialize(ins, jsonSerializerOptions);
var content = new StringContent(json, Encoding.UTF8, "application/json");
string msg = string.Empty;
try
{
var responseM = await _Client.PostAsync(full_uri, content);
msg = await responseM.Content.ReadAsStringAsync();
responseM.EnsureSuccessStatusCode();
string responseBody = await responseM.Content.ReadAsStringAsync();
response.Ins = JsonSerializer.Deserialize<T1>(responseBody);
}
catch (Exception ex)
{
response.IsSuccess = false;
response.Message = ex.Message + Environment.NewLine + msg;
//App.NotificationContainer.AddNotifiaction(new NotificationItem("添加失败", response.Message, NotifiactionType.Error));
}
return response;
}
}
调用方式:
var responseResult = await _httpService.PostAsync("AccelerationFault", accelerationMessage);