Pass through the HTTP response to the client in MVC 6

萝らか妹 提交于 2021-02-10 10:54:25

问题


I am new to Web API and HTTP.

I am using the MVC 6 (beta version). I have a proxy service (Web API) which has a POST method to get response from another service with XML content returned. I need to return the response content to the client since the client can't call the service directly.

// In my proxy service
public HttpResponseMessage Post(String content) 
{
    using ( HttpClient client = new HttpClient() ) {

        .......

        HttpResponseMessage response = client.PostAsync(uri, content).Result;

        // I get everything I need in the "response".

        // How to return the response or it body to the client.
        // return response;
    }
}

II need to return the "response" to the client with no or minimum changes. I tried "return response", or create a new HttpResponseMessage, but I only got something like

{"Headers":[{"Key":"Content-Type","Value":["text/xml"]}]} 

in the body.

So is there a simple way to pass the response back to the client? Thanks.


回答1:


The ASP.NET team is currently working on a "proxy middleware" that does exactly what you're looking for: https://github.com/aspnet/Proxy

Here's how it works internally:

public async Task Invoke(HttpContext context)
{
    var requestMessage = new HttpRequestMessage();
    if (string.Equals(context.Request.Method, "POST", StringComparison.OrdinalIgnoreCase))
    {
        var streamContent = new StreamContent(context.Request.Body);
        requestMessage.Content = streamContent;
    }

    // Copy the request headers
    foreach (var header in context.Request.Headers)
    {
        if (!requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value) && requestMessage.Content != null)
        {
            requestMessage.Content?.Headers.TryAddWithoutValidation(header.Key, header.Value);
        }
    }

    requestMessage.Headers.Host = _options.Host + ":" + _options.Port;
    var uriString = $"{_options.Scheme}://{_options.Host}:{_options.Port}{context.Request.PathBase}{context.Request.Path}{context.Request.QueryString}";
    requestMessage.RequestUri = new Uri(uriString);
    requestMessage.Method = new HttpMethod(context.Request.Method);
    using (var responseMessage = await _httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, context.RequestAborted))
    {
        context.Response.StatusCode = (int)responseMessage.StatusCode;
        foreach (var header in responseMessage.Headers)
        {
            context.Response.Headers.SetValues(header.Key, header.Value.ToArray());
        }

        foreach (var header in responseMessage.Content.Headers)
        {
            context.Response.Headers.SetValues(header.Key, header.Value.ToArray());
        }

        // SendAsync removes chunking from the response. This removes the header so it doesn't expect a chunked response.
        context.Response.Headers.Remove("transfer-encoding");
        await responseMessage.Content.CopyToAsync(context.Response.Body);
    }
}

https://github.com/aspnet/Proxy/blob/dev/src/Microsoft.AspNet.Proxy/ProxyMiddleware.cs



来源:https://stackoverflow.com/questions/31925621/pass-through-the-http-response-to-the-client-in-mvc-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!