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