如何使用请求列表 headers 在 dotnet 中发送 post 请求

How to send a post request in dotnet with a list of request headers

    public static async Task<HttpResponseMessage> Post(string endPoint, string data){
        HttpContent c = new StringContent(data, Encoding.UTF8, "application/json");
        using (var client = new HttpClient())
        {
            HttpRequestMessage request = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = new Uri(VodapayBaseUrl + endPoint),
                Content = c,

            };

            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage result = await client.SendAsync(request).ConfigureAwait(false); // The code fails here

            if (result.IsSuccessStatusCode)
            {
                
                Console.WriteLine("got here");
                return result;
            }
            else
            {
                Console.WriteLine("failled");
                return result;
            }
        }
          
       // return result;
        
    }

这是一个更新版本:

public static async Task Post()
    {
        using (var httpClient = new HttpClient())
        {
            var requestString = "{\"authCode\": \"0000000001Nk1EEhZ3pZ73z700271891\" }";

            httpClient.BaseAddress = new Uri("https://bounties-backend-mini-program.herokuapp.com");

            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var request = new HttpRequestMessage(HttpMethod.Post, $"/api/userInfo");
            request.Content = new StringContent(requestString, System.Text.Encoding.UTF8, "application/json");

            var response = await httpClient.SendAsync(request);
            var responseContent = await response.Content.ReadAsStringAsync();

            Console.WriteLine(JsonConvert.SerializeObject(response.Headers.ToList()));

            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("Successful");
                Console.WriteLine(responseContent);
            }
            else
            {
                Console.WriteLine("Not successful");
            }
        }


    }

    class Program
    {

        private static void Main(string[] args)
        {
           
            Post().Wait();
            Console.WriteLine();

        }
    }
}

有人可以帮忙吗 我是 c# 的新手,对编码也比较陌生。我正在尝试使用 httpclient 发送请求 我需要以 json 格式发送数据 我还需要发送 headers 的列表。我该如何做到这一点以及最后的 return json 数据你的帮助将是 appreciated.I 当我 运行 this:

您的代码不远,这是我在我的一个项目中的示例...

using (var httpClient = new HttpClient())
{
    var requestString = "{\"authCode\": \"0000000001Nk1EEhZ3pZ73z700271891\" }";

    // Setup the HttpClient and make the call and get the relevant data.
    httpClient.BaseAddress = new Uri("https://bounties-backend-mini-program.herokuapp.com");

    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var request = new HttpRequestMessage(HttpMethod.Post, $"/api/userInfo");
    request.Content = new StringContent(requestString, System.Text.Encoding.UTF8, "application/json");

    var response = await httpClient.SendAsync(request);
    var responseContent = await response.Content.ReadAsStringAsync();

    Console.WriteLine(JsonConvert.SerializeObject(response.Headers.ToList()));

    if (response.IsSuccessStatusCode)
    {
        Console.WriteLine("Successful");
        Console.WriteLine(responseContent);
    }
    else
    {
        Console.WriteLine("Not successful");
    }
}

...显然,它对手头的场景有不同程度的思考,但只是根据需要进行调整。