httpclient get 请求+Body

  • 正常情况下 基于HTTP规范 Get请求不应该包含请求正文 即 HttpClient 默认不支持在 GET(httpClient.GetAsync) 请求中发送带有 x-www-form-urlencoded 类型的请求正文

    但是postman是支持的

    且接收方可以从form中获取到body参数

    //python
    @app.route('/jiqingtest2', methods=['GET'])
    def jiqingtest2():
        data = request.form.to_dict()
        print('data:', data)
        return jsonify(data)
    
  • 如果想要与postman达到同样效果 则需要使用 httpclient.SendAsync(HttpRequestMessage )

     var formData = new List<KeyValuePair<string, string>>
            {
                new KeyValuePair<string, string>("param1", "value1"),
                new KeyValuePair<string, string>("param2", "value2")
            };
    using (var client = new HttpClient(httpClientHandler))
    {
        client.Timeout = new TimeSpan(0, 0, timeout); 
        var content = new FormUrlEncodedContent(formData);
        var req = new HttpRequestMessage() { Method = HttpMethod.Get,RequestUri=new Uri(url),Content=content };
        HttpResponseMessage response = await client.SendAsync(req);
        
    }
    
posted @ 2023-10-25 17:57  C余L小R鱼  阅读(426)  评论(0编辑  收藏  举报