最佳答案
我已经写了下面的代码发送标题,后参数。问题是我使用的是 SendAsync,因为我的请求可以是 GET 或 POST。如何将 POST Body 添加到这段代码中,以便如果有任何 POST Body 数据,它将被添加到我发出的请求中,如果它的简单 GET 或 POST 没有 Body,它将以这种方式发送请求。请更新以下代码:
HttpClient client = new HttpClient();
// Add a new Request Message
HttpRequestMessage requestMessage = new HttpRequestMessage(RequestHTTPMethod, ToString());
// Add our custom headers
if (RequestHeader != null)
{
foreach (var item in RequestHeader)
{
requestMessage.Headers.Add(item.Key, item.Value);
}
}
// Add request body
// Send the request to the server
HttpResponseMessage response = await client.SendAsync(requestMessage);
// Get the response
responseString = await response.Content.ReadAsStringAsync();