如何在httpClient中发布参数,而在Postman中使用参数

问题描述 投票:0回答:1

我想用 HttpClient 发布我的参数。 httpclient请求体中的内容是什么?我把它写在正文部分,当我运行该项目时,我收到错误:

我的代码是:

string baseAddress = "https://WebServiceAddress";
Client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
HttpRequestMessage bodyRequest = new HttpRequestMessage(HttpMethod.Post, baseAddress)

bodyRequest.Content = new StringContent(JsonConvert.SerializeObject(Prescriptionrequest), Encoding.UTF8, "application/json");

var responseApi = Client.PostAsync(baseAddress, bodyRequest.Content, new System.Threading.CancellationToken(false));

bodyRequest.Content
的格式应该是什么?

c# web-services dotnet-httpclient
1个回答
0
投票

使用此代码:

HttpClient client = new HttpClient();
var dictionary = new Dictionary<string, string> 
{    
    { "parameter0", "value0" },    
    { "parameter1", "value1" }
};    
var content = new FormUrlEncodedContent(dictionary);    
var response = await client.PostAsync("https://WebServiceAddress", content);    
var responseString = await response.Content.ReadAsStringAsync();
© www.soinside.com 2019 - 2024. All rights reserved.