[]
        
(Showing Draft Content)

第三方网络服务交互

在服务端命令中,推荐使用 HttpClient 来请求网络服务。

using GrapeCity.Forguncy.Commands;
using System.Net.Http;
using System.Threading.Tasks;

namespace MyPlugin
{
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        static HttpClient _httpClient = new HttpClient();
        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            var response = await _httpClient.GetAsync("https://gitee.com/grape-city-software/forguncy-plugin-project-creator/raw/master/README.md");
            if (response.IsSuccessStatusCode)
            {
                var responseContent = await response.Content.ReadAsStringAsync();
                return new ExecuteResult() { Message = responseContent };
            }
            else
            {
                return new ExecuteResult() { ErrCode = (int)response.StatusCode, Message = response.StatusCode.ToString() };
            }
        }

        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
}

代码说明:HttpClient 应该定义为静态对象以节约网络资源。