我从Mightysoft的GitHub档案库中窃取了一些代码,并且能够重现问题标题中提到的同一问题。我有自己的版本,但更改了版本以查看MS的版本是否更好地代理请求。
我的设置如下:
(A)公共面向代理Proxy(B)内部网关➡(C)AppServer
(B)可以成功拨打(C)的电话并获得响应并将其写为:
// Bottom of CopyProxyHttpResponse() method.
var started = response.HasStarted; // experimenting with this based on MS Docs.
using (var responseStream = await responseMessage.Content.ReadAsStreamAsync())
{
await responseStream.CopyToAsync(response.Body, StreamCopyBufferSize, context.RequestAborted);
}
(A)收到对(B)最初发起的请求 ,但由于以下原因,它无法写入正文:
无法写入响应正文,响应已完成
我喜欢调用的用于启动每个请求或跃点的代码对于每个都相同:
public async Task PassThroughAsync(HttpContext context, string url)
{
var targetUri = new Uri(url);
var client = _httpClientFactory.CreateClient();
using (var requestMessage = CreateProxyHttpRequest(context,targetUri))
{
using (var responseMessage = await client.SendAsync(requestMessage,
HttpCompletionOption.ResponseHeadersRead, context.RequestAborted))
{
await CopyProxyHttpResponse(context, responseMessage);
}
}
}
我正在使用的框架是: Netcore 2.2.6
我尝试跳过CopyToAsync()
方法调用以查看返回给Postman的内容,并且在VS内没有任何错误,但响应主体为空,可能是由于响应处理不正确/格式错误而导致的标头不正确。
问题:我会采用这种正确的方式吗?