i have this code and worked
string userName = "username";
string password = "password";
string clientSecret = "clientSecretCode";
var client = new RestClient("url");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("undefined",
$"grant_type=password&username={userName}&password={password}&client_id=mmp-web-api&client_secret={clientSecret}&scope=openid%20mmp-webpanel",
ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var result = JsonConvert.DeserializeObject<TokenOutputDto>(response.Content);
TokenOutputDto is :
public class TokenOutputDto
{
public string access_token { get; set; }
}
how i can convert this code to httpclient(run without RestSharp)
string userName = "username";
string password = "password";
string clientSecret = "clientSecretCode";
HttpClient httpClient = new HttpClient();
Uri requestUri = new Uri("Your Complete URL");
httpClient.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));//ACCEPT header
// Parameter
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("undefined", $"grant_type=password&username={userName}&password={password}&client_id=mmp-web-api&client_secret={clientSecret}&scope=openid%20mmp-webpanel")
});
var response = httpClient.PostAsync(requestUri.ToString(), content).Result;
var token = response.Content.ReadAsStringAsync().Result;
var result = JsonConvert.DeserializeObject<TokenOutputDto>(token);
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.