【问题标题】:How to pass request content with HttpClient GetAsync method in c#如何在 c# 中使用 HttpClient GetAsync 方法传递请求内容
【发布时间】:2020-05-15 14:42:25
【问题描述】:

如何在HttpClient.GetAsync 方法中传递请求内容?我需要根据请求内容获取数据。

[HttpGet]
public async Task<HttpResponseMessage> QuickSearch()
{
    try
    {
        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Clear();
            HttpResponseMessage response =await client.GetAsync("http://localhost:8080/document/quicksearch");

            if (response.IsSuccessStatusCode)
            {
                Console.Write("Success");
            }

【问题讨论】:

  • 我正在从 java api 连接到 angular ui。
  • 扔掉我的 .Net api
  • 什么是“请求内容”?您是在问如何为正文提供 GET 请求?因为如果你是,你不应该。 GET 不应该需要正文。

标签: c#


【解决方案1】:

如果您使用的是 .NET Core,标准的 HttpClient 可以开箱即用地执行此操作。例如,发送带有 JSON 正文的 GET 请求:

HttpClient client = ...

...

var request = new HttpRequestMessage
{
    Method = HttpMethod.Get,
    RequestUri = new Uri("some url"),
    Content = new StringContent("some json", Encoding.UTF8, ContentType.Json),
};

var response = await client.SendAsync(request).ConfigureAwait(false);
response.EnsureSuccessStatusCode();

var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

【讨论】:

    【解决方案2】:

    我假设您的“请求内容”将是 POST 数据,不是吗?

    如果您使用标准表单内容的方式发送它,您首先必须构建内容:

    var content = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("username", "theperplexedone"),
                    new KeyValuePair<string, string>("password", "mypassword123"),
                });
    

    然后改用PostAsync 提交:

    var response = await client.PostAsync("http://localhost:8080/document/quicksearch", content);
    

    【讨论】:

    • 这里的事实是我的参数没有预定义。我必须获得一个参数,但属性名称可以是任何东西,因此我无法在我的模型类中定义它。我必须获取属性名称和类型并作为查询字符串传递。它是为 GET
    【解决方案3】:

    如果要发送内容,则需要将其作为查询字符串发送(根据您的 API 路由)

    HttpResponseMessage response =await client.GetAsync("http://localhost:8080/document/quicksearch/paramname=<dynamicName>&paramValue=<dynamicValue>");
    

    在 API 中检查“paramName”和“paramValue”

    【讨论】:

    • 这里的事实是我的参数没有预定义。我必须获得一个参数,但属性名称可以是任何东西,因此我无法在我的模型类中定义它。我必须获取属性名称和类型并作为查询字符串传递
    • 那么您需要使用 PostAsync 或发送带有 name 和 value 的参数。查看编辑后的答案
    【解决方案4】:

    大家好,谢谢你们的cmets,我得到了解决方案

    [HttpGet]
            public async Task<HttpResponseMessage> QuickSearch(HttpRequestMessage Query)
            {
                Debugger.Launch();
                try
                {
                    using (HttpClient client = new HttpClient())
                    {
                        client.DefaultRequestHeaders.Accept.Clear();
    
                        Console.WriteLine(Query);
                        HttpResponseMessage response = await client.GetAsync("http://localhost:8080/document/quicksearch/"+ Query.RequestUri.Query);
    
                        if (response.IsSuccessStatusCode)
                        {
                            Console.Write("Success");
                        }
                        else
                        {
                            Console.Write("Failure");
                        }
    
                        return response;
                    }
                }
                catch (Exception e)
                {
    
                    throw e;
                }
    

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      相关资源
      最近更新 更多