64 lines
1.4 KiB
C#
64 lines
1.4 KiB
C#
using MAX.Core;
|
|
using MAX.Core.DependencyInjection;
|
|
using MAX.Core.Infrastructure;
|
|
using RestSharp;
|
|
|
|
namespace Trade.Client;
|
|
|
|
public class Startup : IStartup
|
|
{
|
|
public Startup()
|
|
{
|
|
|
|
}
|
|
|
|
public int Priority => IntPriority.Normal;
|
|
|
|
public void Initialize()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void Register(IServiceRegistrar registrar)
|
|
{
|
|
|
|
}
|
|
|
|
async Task ss()
|
|
{
|
|
// 1. 创建客户端
|
|
var client = new RestClient("https://api.example.com");
|
|
|
|
// 2. 创建请求,并指定资源路径
|
|
var request = new RestRequest("users/{id}", Method.GET);
|
|
|
|
// 3. 添加请求参数
|
|
// 添加 URL 路径参数,替换 {id}
|
|
request.AddUrlSegment("id", "123");
|
|
// 添加 URL 查询参数
|
|
request.AddQueryParameter("include", "details");
|
|
// 添加请求头
|
|
request.AddHeader("Authorization", "Bearer your-token");
|
|
request.Method = Method.POST;
|
|
|
|
// 4. 执行请求,并获取响应
|
|
// 这里 Execute 是同步方法,如果是异步场景也可以用 ExecuteAsync
|
|
IRestResponse response = await client.ExecuteAsync(request);
|
|
|
|
// 5. 处理响应
|
|
if (response.IsSuccessful)
|
|
{
|
|
// 响应内容可以通过 response.Content 获取
|
|
var content = response.Content;
|
|
// 或者通过 RestSharp 自带的序列化器直接反序列化成对象
|
|
// var user = client.Execute<User>(request).Data;
|
|
Console.WriteLine(content);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"请求失败: {response.StatusCode}, {response.ErrorMessage}");
|
|
}
|
|
|
|
}
|
|
}
|