0

我想构建一个 HTTP 代理,将所有请求路由到另一个 HTTP 服务。有人对如何使用 Web-API 实现这一点有什么好的建议吗?

我的情况是我们有两组服务。一个是外部可访问的,一个不是。我想从外部服务提供一个路由器来调用所有逻辑和配置所在的内部服务。

4

2 回答 2

2

是的,这很容易做到。只需创建一个派生自 DelegatingHandler 的类,重写 SendAsync 并将其添加到配置对象的 MessageHandlers 集合中。

这是我创建的一个自托管控制台应用程序代理,可将 JSON 转换为 XML,反之亦然。它应该让您了解它是如何完成的。

class Program
{
    static void Main(string[] args)
    {
        var config = new HttpSelfHostConfiguration("http://localhost:8081");
        var server = new HttpSelfHostServer(config);


        var originServerUri = new Uri("http://example.org/origin-server/");      
        config.MessageHandlers.Add(new Converter(originServerUri));

        server.OpenAsync().Wait();

        Console.Read();

        server.CloseAsync().Wait();

    }
}


public class Converter : DelegatingHandler
{
    private HttpClient _HttpClient = new HttpClient();
    private Uri _OriginServer;
    public Converter(Uri originServer)
    {
        _OriginServer = originServer;
    }
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
    {
           Console.WriteLine(new HttpMessageContent(request).ReadAsStringAsync().Result);


            var newRequest = CreateNewRequest(request);

            var t = _HttpClient.SendAsync(newRequest);

            await t;

            if (t.IsCompleted)
            {
                try
                {
                    var response = CreateNewResponse(t.Result);
                    Console.WriteLine("--->");
                    Console.WriteLine(new HttpMessageContent(response).ReadAsStringAsync().Result);
                    return response;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(ex.Message)};

                }
            }
            else
            {
                return new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(t.Exception.Message)};
            }
    }

    private HttpRequestMessage CreateNewRequest(HttpRequestMessage request)
    {
        var newRequest = new HttpRequestMessage();

        newRequest.Headers.Clear();
        foreach (var header in request.Headers)
        {
            newRequest.Headers.Add(header.Key, header.Value);
        }
        if (request.Content.Headers.ContentLength != 0)
        {
            newRequest.Content = TranslateContent(request.Content);
        }
        newRequest.Headers.Host = null;
        newRequest.Method = request.Method;
        newRequest.RequestUri = new Uri(_OriginServer, request.RequestUri.PathAndQuery);
        return newRequest;
    }

    private HttpResponseMessage CreateNewResponse(HttpResponseMessage response)
    {
        response.Content = TranslateContent(response.Content);
        return response;
    }

    private HttpContent TranslateContent(HttpContent httpContent)
    {
        var mediatype = httpContent.Headers.ContentType.MediaType;
        if (mediatype.Contains("+xml"))
        {
            return TranslateXmlToJson(httpContent);
        }
        else
        {
            return TranslateJsonToXml(httpContent);
        }
    }

    private HttpContent TranslateJsonToXml(HttpContent content)
    {
        var mediatype = content.Headers.ContentType.MediaType;

        string json = content.ReadAsStringAsync().Result;
        XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
        return new StringContent(doc.OuterXml, Encoding.UTF8, mediatype.Replace("+json", "+xml"));
    }

    private HttpContent TranslateXmlToJson(HttpContent content)
    {
        var mediatype = content.Headers.ContentType.MediaType;
        XmlDocument doc = new XmlDocument();
        doc.Load(content.ReadAsStreamAsync().Result);

        string jsonText = JsonConvert.SerializeXmlNode(doc);
        return new StringContent(jsonText, Encoding.UTF8, mediatype.Replace("+xml", "+json"));
    }


}
于 2012-10-17T22:48:13.153 回答
0

Thanks for the responses guys. In the end I decided to go with an IHttpHandler as the service where the proxy is needed was currently not using web-api. This was done by doing something like:

public class MyProxy : IHttpHandler
{
    private static readonly string CoreServicesUrl = "localhost:1234";

    public void ProcessRequest(HttpContext context)
    {
        string serviceUrl = context.Request.Headers.Get("ServiceUrl");
        if (serviceUrl == null)
        {
            context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return;
        }

        string url = string.Format("http://{0}/api/{1}", CoreServicesUrl, serviceUrl);
        HttpResponseMessage response = RouteRequest(context, url);

        // This will handle both successful and unsuccessful responses
        context.Response.StatusCode = (int)response.StatusCode;
        var content = response.Content.ReadAsStringAsync().Result;
        context.Response.Write(content);
    }

    private static HttpResponseMessage RouteRequest(HttpContext context, string url)
    {
        var client = new HttpClient();

        if (context.Request.HttpMethod == "GET")
            return client.GetAsync(url).Result;

        if (context.Request.HttpMethod == "POST")
        {
            var stream = context.Request.GetBufferlessInputStream();
            var sr = new StreamReader(stream);
            var body = sr.ReadToEnd();
            try
            {
                return client.PostAsync(url, new StringContent(body, Encoding.UTF8, "application/json")).Result;
            }
            catch (Exception ex)
            {
                var response = new HttpResponseMessage(HttpStatusCode.InternalServerError) {ReasonPhrase = ex.Message};
                return response;
            }
        }

        // Other HTTP verbs are not supported
        return new HttpResponseMessage(HttpStatusCode.NotImplemented);
    }        

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
于 2012-10-19T01:23:50.550 回答