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 @   C余L小R鱼  阅读(496)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示