httpcline转发,在“原样”转发整个接收到的请求。使用HttpClient

I have a thin frontend API web service that does some preprocessing on the received data and then sends the data to my backend API web service using HttpClient.

There are some complex cases when a request contains multipart data with JSON and files, and I don't want to parse it at all in the frontend. The backend will do the job.

So, I would like to take the request "as is" - as raw as possible (not caring about its contents and whether it's multipart or not) and just forward it to the backend API.

I tried the following:

var msg = new HttpRequestMessage(HttpMethod.Post, resourceUrl);

msg.Content = new StreamContent(request.Body);

var apiResponse = await _httpClient.SendAsync(msg);

but the backend web service receives an empty request body with 0 length.

How do I forward the entire request body without having to analyze it and reassemble a new request body?

解决方案

You need to set msg.Content.ContentLength for this to work correctly. It's also a good idea to copy ContentType and other content headers from the request into msg.Content, so that your backend service knows how to parse it.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值