6

.NET で単純なパススルー プロキシを作成しようとしています。

外部ドメイン (http://exampleapi.com) でホストされている REST API があります。

そして、アプリケーションに送信されたすべてのリクエスト (get、post など) を通過させたいと考えています。JSONP はオプションではありません。

したがって、私が求める場合 GET localhost:1234/api/pages => GET http://exampleapi.com/pages 同様に私がPOST localhost:1234/api/pages => POST http://exampleapi.com/pages

私が抱えている大きな問題であり、他の場所では見つけられないように見えるのは、この JSON を解析したくないということです。検索したものはすべて を中心にしてHttpClientいるようですが、正しく使用する方法がわかりません。

これが私がこれまでに持っているものです:

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");
}
4

1 に答える 1

2

HTTP サーバーを作成し、リクエストを受信すると、コードがそのリクエストから情報を取得し、新しいサーバーへの新しいリクエストを生成し、レスポンスを受信して​​、レスポンスを元のクライアントに送り返す必要があります。 .

クライアント -> C# サーバー -> Rest API サーバー

オープン ソースのサンプル HTTP サーバーを次に示します。 https://github.com/kayakhttp/kayak

于 2012-11-06T23:17:15.650 に答える