将查询字符串传递给HttpRequestMessage

时间:2020-11-02 13:48:39

标签: c# httpclient

我想将查询字符串传递给请求,

如果我在请求上键入它本身可以工作,但是如果我将其添加到请求中,则内容失败,

这是我的代码

 public async Task<Product> GetProducts(
        IEnumerable<string> products,
        DateTime startDate,
        DateTime endDate)
    {
        var client = new HttpClient();
        client.BaseAddress = new Uri(this.productDataAddress);
        client.DefaultRequestHeaders
            .Accept
            .Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var request = new HttpRequestMessage(HttpMethod.Get, "/products/datetime");
        request.Headers.Add("apad-accept", "application/json");

        request.Content = new StringContent(
            "{\"productIds\":\"PR00001\"}",
            Encoding.UTF8,
            "application/json");

        var response = await client.SendAsync(request);

        var content = await response.Content.ReadAsStringAsync();

        var result = JsonConvert.DeserializeObject<Product>(content);

        return result;
    }

如果我修改var request = new HttpRequestMessage(HttpMethod.Get, "/products/datetime?productIds=PR00001");行,则可以

我有google它,但不起作用,关于如何构建查询字符串的任何想法?

谢谢

0 个答案:

没有答案