我无法在WixSharp安装程序中使用RestSharp,是否可以使用HttpClient或WebClient完成此操作?

问题描述

private async Task<AuthenticationToken> GetToken()
    {

        string username = loginDialog.username;
        string password = loginDialog.password;
        string requestString = $"Service/Login";
        RestRequest request = new RestRequest(requestString,Method.POST);
        request.AddParameter("username",username);
        request.AddParameter("password",password);
        IRestResponse<AuthenticationToken> response = await _client.ExecuteAsync<AuthenticationToken>(request);
        return response.Data;
    }

我在解决方案中的一些项目中使用RestSharp。我无法在安装程序中使用它,因为WixSharp在RestSharp上无法正常工作。我需要使用WebClient或HttpClient来获得与使用RestSharp库使用此方法所获得的响应相同的响应。有人能帮忙吗?

解决方法

好的,试试这个

var httpClient = new HttpClient();
var headers = httpClient.DefaultRequestHeaders;
headers.Add("Content-Tpye","application/form-url-encoded");
string requestParams = string.Format("grant_type=password&username={0}&password={1}",username,password);
HttpContent content = new StringContent(requestParams);
var response = httpClient.PostAsync(requestString,content);
var responseContent = await response.Result.Content.ReadAsStringAsync();

            

然后,您可以使用responseContent或任何JSON库在NewtonSoft上使用JSON解串器。

,

在需要Wixsharm运行时需要一些外部程序集的情况下,只需将其添加如下:

var proj = ManagedProject()
proj.DefaultRefAssemblies.Add("Restsharp.dll")

它可以在运行时加载并且可以访问使用。

P.S。不要忘记在构建器可执行文件的输出位置检查程序集是否存在。