通过HttpClient将空主体发布到REST API [英] Post an empty body to REST API via HttpClient

查看:139
本文介绍了通过HttpClient将空主体发布到REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要调用的API要求我执行POST,但正文为空.我是使用WCF Web API HttpClient的新手,我似乎找不到要写带有空正文的帖子的写代码.我找到了对某些HttpContent.CreateEmpty()方法的引用,但是由于我似乎找不到该方法,因此我认为这不是针对Web API HttpClient代码的.

The API I'm trying to call requires that I do a POST but with an empty body. I'm new to using the WCF Web API HttpClient and I can't seem to find out the write code that would do a post with an empty body. I find references to some HttpContent.CreateEmpty() method, but I don't think that is for the Web API HttpClient code since I can't seem to find that method.

推荐答案

使用从HttpContent派生的StringContentObjectContent,也可以将null用作HttpContent:

Use StringContent or ObjectContent which derive from HttpContent or you can use null as HttpContent:

var response = await client.PostAsync(requestUri, null);

这篇关于通过HttpClient将空主体发布到REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆