Try to implement a method with the following code:
public async Task<string> PostFormUrlEncoded(string url, Dictionary<string, string> postData)
{
using (var httpClient = new HttpClient())
{
using (var content = new FormUrlEncodedContent(postData))
{
content.Headers.Clear();
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
HttpResponseMessage response = await httpClient.PostAsync(url, content);
return await response.Content.ReadAsStringAsync();
}
}
}
Calling the method using an anonymous type would look like this:
using Newtonsoft.Json;
Dictionary<string, string> dictionary = new Dictionary<string, string>();
var objetoAnonimo = new
{
action = new
{
name = "user_login",
parameters = new
{
login_name = "user",
password = "123"
}
}
};
dictionary.Add("parametro1", System.Web.HttpUtility.UrlEncode(JsonConvert.SerializeObject(objetoAnonimo)));
string jsonReturn = await PostFormUrlEncoded<string>("http://urlapi.com", dictionary);
If you decide to create a class structure to represent that template, create these two classes below:
public class Action
{
public string Name { get; set; }
public Parameter Parameters { get; set; }
}
public class Parameter
{
public string Login_Name { get; set; }
public string Password { get; set; }
}
At the time of calling the method it would look like this:
using Newtonsoft.Json;
Dictionary<string, string> dictionary = new Dictionary<string, string>();
Action objeto = new Action();
objeto.Name = "user_login";
objeto.Parameters = new Parameter();
objeto.Parameters.Login_Name = "user";
objeto.Parameters.Password= "123";
dictionary.Add("parametro1", System.Web.HttpUtility.UrlEncode(JsonConvert.SerializeObject(objeto)));
string jsonReturn = await PostFormUrlEncoded<string>("http://urlapi.com", dictionary);