How to use System.Net.HttpClient to post a complex type?

前端 未结 9 533
感动是毒
感动是毒 2020-11-29 16:31

I have a custom complex type that I want to work with using Web API.

public class Widget
{
    public int ID { get; set; }
    public string Name { get; set;         


        
9条回答
  •  心在旅途
    2020-11-29 16:51

    Note that if you are using a Portable Class Library, HttpClient will not have PostAsJsonAsync method. To post a content as JSON using a Portable Class Library, you will have to do this:

    HttpClient client = new HttpClient();
    HttpContent contentPost = new StringContent(argsAsJson, Encoding.UTF8, 
    "application/json");
    
    await client.PostAsync(new Uri(wsUrl), contentPost).ContinueWith(
    (postTask) => postTask.Result.EnsureSuccessStatusCode());
    

提交回复
热议问题