6

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");
}
Jon Jaques
  • 4,262
  • 2
  • 23
  • 25

1 Answers1

2

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. https://github.com/kayakhttp/kayak

Jpsy
  • 20,077
  • 7
  • 118
  • 115
Walk
  • 1,136
  • 8
  • 8
  • Thanks for your answer. I was operating under the assumption that the C# server could make the external request itself, but is that not the case? I've gotten GETs and DELETEs working via RestSharp (not in my example), it's the POST/PUT that are giving me problems. – Jon Jaques Nov 06 '12 at 23:29
  • In my example, jsonInput successfully captures any json I post to it, I guess at this point I just need to figure out how to make an external POST request. The problem I face is everything you find online is dealing with serializing JSON from existing objects. – Jon Jaques Nov 06 '12 at 23:32
  • For a true proxy, you need to be able to take the interaction and process it, using an HTTP server would be the easiest way to do this. A legit proxy is just a listening HTTP server than then makes an HTTP request on your behalf and then returns the results. You can go through the trouble of creating a `TcpListener` and parsing out the headers, but why reinvent the wheel. – Walk Nov 06 '12 at 23:32
  • For a POST message, you'll have to add some headers to the request. – Walk Nov 06 '12 at 23:35
  • Here's an example of doing an HTTP post using C#: http://stackoverflow.com/questions/4015324/http-request-with-post – Walk Nov 07 '12 at 19:18
  • Link in answer corrected! KayakHttp on Githup: https://github.com/kayakhttp/kayak – Jpsy Sep 11 '18 at 07:41