c# - 发送带有请求正文的http get c#
问题描述
我想发送一个带有请求正文的 HTTP GET。我知道关于是否应该这样做有很多激烈的争论,但我对争论不感兴趣,我只是想这样做。我正在使用 C# 和 ASP.NET,我的代码如下。不幸的是,它会引发异常“无法发送具有此动词类型的内容主体”。请,任何有关如何完成这项工作的帮助将不胜感激!
// Serialize our concrete class into a JSON String
var stringPayload = JsonConvert.SerializeObject(memRequest);
// Wrap our JSON inside a StringContent which then can be used by the HttpClient class
var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
using (var httpClient = new HttpClient())
{
HttpRequestMessage request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = u,
Content = httpContent
};
var result = httpClient.SendAsync(request).Result;
result.EnsureSuccessStatusCode();
var responseBody = result.Content.ReadAsStringAsync().ConfigureAwait(false);
解决方案
推荐阅读
- c - 打印指针值 NULL
- scala - Scala中的嵌套Monads组合
- javascript - 从 json 响应动态创建模式
- vue.js - 使用 v2 api 和 vue 自定义 wordpress 分类法
- angular - 我想在单击按钮时显示特定订单的详细信息,但它会在每个订单上呈现,因为这是在 for 循环中
- authentication - Ember-simple-auth 和 ember-fetch 在 fetch 查询中没有授权
- excel - Excel使用下拉菜单创建摘要,使用sumif公式从另一张表中获取数据
- json - 如何将json数据转换为记录并插入到sql server中的使用中
- python - 使用 Python 保存多个图像
- google-cloud-platform - GKE - Stackdriver Kubernetes 监控