我需要将 Microsoft.AspNetCore.Http.HttpRequest 从AspNetCore上下文转换为 HttpRequestMessage 以传递给HttpClient。有没有一种简单的方法来实现这一目标?或者任何实现这一点的提示都会非常有用。
更新 我想将请求转换为消息,但我想更改目标网址,我只想将请求重定向到另一个服务器。
答案 0 :(得分:10)
尝试 Web API兼容性Shim
HttpRequestMessageFeature hreqmf = new HttpRequestMessageFeature(context);
HttpRequestMessage httpRequestMessage = hreqmf.HttpRequestMessage;
或者您可能会受到 Microsoft.AspNetCore.Proxy
的启发These extensions到httpContext可能会派上用场。
答案 1 :(得分:5)
我最终这样做了:
public static class RequestTranscriptHelpers
{
public static HttpRequestMessage ToHttpRequestMessage(this HttpRequest req)
=> new HttpRequestMessage()
.SetMethod(req)
.SetAbsoluteUri(req)
.SetHeaders(req)
.SetContent(req)
.SetContentType(req);
private static HttpRequestMessage SetAbsoluteUri(this HttpRequestMessage msg, HttpRequest req)
=> msg.Set(m => m.RequestUri = new UriBuilder
{
Scheme = req.Scheme,
Host = req.Host.Host,
Port = req.Host.Port.Value,
Path = req.PathBase.Add(req.Path),
Query = req.QueryString.ToString()
}.Uri);
private static HttpRequestMessage SetMethod(this HttpRequestMessage msg, HttpRequest req)
=> msg.Set(m => m.Method = new HttpMethod(req.Method));
private static HttpRequestMessage SetHeaders(this HttpRequestMessage msg, HttpRequest req)
=> req.Headers.Aggregate(msg, (acc, h) => acc.Set(m => m.Headers.TryAddWithoutValidation(h.Key, h.Value.AsEnumerable())));
private static HttpRequestMessage SetContent(this HttpRequestMessage msg, HttpRequest req)
=> msg.Set(m => m.Content = new StreamContent(req.Body));
private static HttpRequestMessage SetContentType(this HttpRequestMessage msg, HttpRequest req)
=> msg.Set(m => m.Content.Headers.Add("Content-Type", req.ContentType), applyIf: req.Headers.ContainsKey("Content-Type"));
private static HttpRequestMessage Set(this HttpRequestMessage msg, Action<HttpRequestMessage> config, bool applyIf = true)
{
if (applyIf)
{
config.Invoke(msg);
}
return msg;
}
}
public static class ResponseTranscriptHelpers
{
public static async Task FromHttpResponseMessage(this HttpResponse resp, HttpResponseMessage msg)
{
resp.SetStatusCode(msg)
.SetHeaders(msg)
.SetContentType(msg);
await resp.SetBodyAsync(msg);
}
private static HttpResponse SetStatusCode(this HttpResponse resp, HttpResponseMessage msg)
=> resp.Set(r => r.StatusCode = (int)msg.StatusCode);
private static HttpResponse SetHeaders(this HttpResponse resp, HttpResponseMessage msg)
=> msg.Headers.Aggregate(resp, (acc, h) => acc.Set(r => r.Headers[h.Key] = new StringValues(h.Value.ToArray())));
private static async Task<HttpResponse> SetBodyAsync(this HttpResponse resp, HttpResponseMessage msg)
{
using (var stream = await msg.Content.ReadAsStreamAsync())
using (var reader = new StreamReader(stream))
{
var content = await reader.ReadToEndAsync();
return resp.Set(async r => await r.WriteAsync(content));
}
}
private static HttpResponse SetContentType(this HttpResponse resp, HttpResponseMessage msg)
=> resp.Set(r => r.ContentType = msg.Content.Headers.GetValues("Content-Type").Single(), applyIf: msg.Content.Headers.Contains("Content-Type"));
private static HttpResponse Set(this HttpResponse msg, Action<HttpResponse> config, bool applyIf = true)
{
if (applyIf)
{
config.Invoke(msg);
}
return msg;
}
}
我没有尝试 @Strenkor En'Triel 回复,因为我先找到了这个解决方案,但我会尝试一下。
答案 2 :(得分:0)
IHttpRequestMessageFeature
和Microsoft.AspNetCore.Proxy
均已停用。 IHttpRequestMessageFeature
已从ASP.NET Core 3.0中删除。
Microsoft现在具有基于ASP.NET Core基础结构的新的反向代理nuget程序包Microsoft.ReverseProxy。根据他们的文档,IHttpProxy
用于直接代理方案可以:
用作传入的AspNetCore与传出的System.Net.Http请求之间的核心代理适配器。它处理从
HttpRequestMessage
创建HttpContext
,发送并中继响应的机制。
这里是an example。