如何代理一个REST API使用.NET 4REST、API、NET

2023-09-03 17:44:00 作者:彻夜笙歌古城门

我试着写一个简单的,直通的.NET代理。

我在主持一些外部域中的REST API(http://exampleapi.com),

和我想通过发送到我的应用程序(GET,POST等)的所有请求。 JSONP不是一个选项。

所以,如果我问获取本地主机:1234 / API /页 => GET http://exampleapi.com/pages 同样,如果我 POST本地主机:1234 / API /页 => POST http://exampleapi.com/pages

最大的问题我有,而我似乎无法找到其他地方 - 就是我的不的要分析此JSON。一切我已经通过搜查,似乎围绕着的HttpClient ,但我似乎无法弄清楚如何正确地使用它。

这是我到目前为止有:

 公共ContentResult类型代理()
{
    //抓住路径从/ API / *
    VAR路径= Request.RawUrl.ToString()子串(4)。
    变种目标=新UriBuilder(HTTP,exampleapi.com,25001);
    VaR方法= Request.HttpMethod;

    VAR的客户=新的HttpClient();
    client.BaseAddress = target.Uri;

    //需要获得充满了回应。
    字符串内容;

    HTT presponseMessage响应;
    开关(方法)
    {
        案POST:
        案PUT:
            StreamReader的读者=新的StreamReader(Request.InputStream);
            变种jsonInput = reader.ReadToEnd();

            //完全丢在这里。
            client.PostAsync(路径,jsonInput);

            打破;
        案删除:
            client.DeleteAsync(路径);
            打破;
        案GET:
        默认:
            //需要捕获客户数据
            client.GetAsync(路径);
            打破;
    }

    返回内容(内容,应用/ JSON);
}
 

解决方案 推荐 5 款好用的REST API工具

您需要创建一个HTTP服务器,接收请求,那么你的code将拉动信息指出请求,并生成新的要求到新的服务器,接收响应,然后发送响应返回给原来的客户端。

客户端 - > C#服务器 - > REST API服务器

下面是一个示例HTTP服务器是开源的。 http://kayakhttp.com/

I'm trying to write a simple, pass-through proxy in .NET.

I have a REST api hosted at some external domain (http://exampleapi.com),

And I want to pass through all requests sent to my application (get, post, etc). JSONP isn't an option.

So if I ask for GET localhost:1234/api/pages => GET http://exampleapi.com/pages Likewise if I POST localhost:1234/api/pages => POST http://exampleapi.com/pages

The big problem I have, and what I can't seem to find elsewhere - is that I don't want to parse this JSON. Everything I've searched through seems to center around HttpClient, but I can't seem to figure out how to use it correctly.

Here's what I have so far:

public ContentResult Proxy()
{
    // Grab the path from /api/*
    var path = Request.RawUrl.ToString().Substring(4);
    var target = new UriBuilder("http", "exampleapi.com", 25001);
    var method = Request.HttpMethod;

    var client = new HttpClient();
    client.BaseAddress = target.Uri;

    // Needs to get filled with response.
    string content;

    HttpResponseMessage response;
    switch (method)
    {
        case "POST":
        case "PUT":
            StreamReader reader = new StreamReader(Request.InputStream);
            var jsonInput = reader.ReadToEnd();

            // Totally lost here.
            client.PostAsync(path, jsonInput);

            break;
        case "DELETE":
            client.DeleteAsync(path);
            break;
        case "GET":
        default:
            // need to capture client data
            client.GetAsync(path);
            break;
    }

    return Content(content, "application/json");
}

解决方案

You'll need to create a HTTP Server, receive the request, then your code will pull the information out of that request, and generate a new request to the new server, receive the response, and then send the response back to the original client.

Client -> C# Server -> Rest API server

Here's a sample HTTP Server that is open source. http://kayakhttp.com/