Files
Web_BLVLOG_Server_Mvc_Prod/Common/Http.cs

101 lines
3.2 KiB
C#
Raw Normal View History

2025-11-20 16:20:04 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RestSharp;
namespace Common
{
public class ReturnInfo
{
public bool isok { set; get; } // 是否成功true成功
public string message { set; get; } // 传递接口信息or报错信息
public int status { set; get; } // 服务器报错信息正确为200错误404/500等
public object response { set; get; } // 获取到的信息本体若报错则为null
}
public class Http
{
public static string BaseUrl = "https://www.boonlive-rcu.com";
static public async Task<ReturnInfo> HttpPostSendData(Object data, string url)
{
ReturnInfo r = new ReturnInfo();
try
{
var options = new RestClientOptions(BaseUrl);
var client = new RestClient(options);
string str = System.Text.Json.JsonSerializer.Serialize(data);
var request = new RestRequest(url);
request.AddJsonBody(str);
RestResponse response = await client.PostAsync(request);
string? str1 = response.Content;
r.isok = true;
r.response = str1;
}
catch (Exception ex)
{
r.isok = false;
r.response = ex.Message;
}
return r;
}
static public async Task<ReturnInfo> HttpPostFormSendData(Dictionary<string, string> data, string url)
{
ReturnInfo r = new ReturnInfo();
try
{
var options = new RestClientOptions(BaseUrl);
var client = new RestClient(options);
string str = System.Text.Json.JsonSerializer.Serialize(data);
var request = new RestRequest(url);
foreach (var item in data)
{
request.AddParameter(item.Key, item.Value);
}
RestResponse response = await client.PostAsync(request);
string? str1 = response.Content;
r.isok = true;
r.response = str1;
}
catch (Exception ex)
{
r.isok = false;
r.response = ex.Message;
}
return r;
}
static public async Task<ReturnInfo> HttpGetSendData(Object data, string url)
{
ReturnInfo r = new ReturnInfo();
try
{
var options = new RestClientOptions(BaseUrl);
var client = new RestClient(options);
string str = System.Text.Json.JsonSerializer.Serialize(data);
var request = new RestRequest(url);
request.AddParameter("jsonData", str);
RestResponse response = await client.GetAsync(request);
string? str1 = response.Content;
r.isok = true;
r.response = str1;
}
catch (Exception ex)
{
r.isok = false;
r.response = ex.Message;
}
return r;
}
}
}