How do I set up HttpContent for my HttpClient PostAsync second parameter?

前端 未结 2 1913
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 17:07
public static async Task GetData(string url, string data)
{
    UriBuilder fullUri = new UriBuilder(url);

    if (!string.IsNullOrEmpty(data))
                


        
2条回答
  •  醉酒成梦
    2020-11-29 17:45

    To add to Preston's answer, here's the complete list of the HttpContent derived classes available in the standard library:

    Credit: https://pfelix.wordpress.com/2012/01/16/the-new-system-net-http-classes-message-content/

    There's also a supposed ObjectContent but I was unable to find it in ASP.NET Core.

    Of course, you could skip the whole HttpContent thing all together with Microsoft.AspNet.WebApi.Client extensions (you'll have to do an import to get it to work in ASP.NET Core for now: https://github.com/aspnet/Home/issues/1558) and then you can do things like:

    var response = await client.PostAsJsonAsync("AddNewArticle", new Article
    {
        Title = "New Article Title",
        Body = "New Article Body"
    });
    

提交回复
热议问题