简体   繁体   中英

I cannot use RestSharp In my WixSharp installer, is it possible to accomplish this with HttpClient or 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;
    }

I am using RestSharp in a few projects within my solution. I can not use it within the installer, WixSharp isn't working well with RestSharp. I need to use WebClient or HttpClient to achieve the same response as I get with this Method using the RestSharp library. Is anyone able to help?

Sure, try this

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();

            

Then you can use JSON deserializer on responseContent using NewtonSoft or any JSON library.

In the case of requirement some external assembly for Wixsharm runtime just add it like below:

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

And it will be avaliable for load in runtime and accessable for use.

PS Dont forget to check assembly existence in your builder executable file output location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM