C# HttpClient Post Data Remote Server
All Code
public class Product
{
public string Name { get; set; }
public string CategoryId { get; set; }
}
class Program
{
public static async Task<bool> PostData(string Name, string CategoryId)
{
Product product = new Product();
product.Name = Name;
product.CategoryId = CategoryId;
string url = “http://xyz.com";
string json = JsonConvert.SerializeObject(product);
StringContent data = new StringContent(json, Encoding.UTF8, “application/json”);
var client = new HttpClient();
var response = await client.PostAsync(url, data);
if (response.Content != null) {
var responseContent = await response.Content.ReadAsStringAsync();
}
return false;
}
static async Task Main(string[] args)
{
bool data = PostData(“Product1”, “3”).Result;
}
}