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

前端 未结 9 538
感动是毒
感动是毒 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条回答
  •  旧时难觅i
    2020-11-29 16:52

    This is the code I wound up with, based upon the other answers here. This is for an HttpPost that receives and responds with complex types:

    Task response = httpClient.PostAsJsonAsync(
                           strMyHttpPostURL,
                           new MyComplexObject { Param1 = param1, Param2 = param2}).ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode());
                        //debug:
                        //String s = response.Result.Content.ReadAsStringAsync().Result;
                        MyOtherComplexType moct = (MyOtherComplexType)JsonConvert.DeserializeObject(response.Result.Content.ReadAsStringAsync().Result, typeof(MyOtherComplexType));
    

提交回复
热议问题