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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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.
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.
For a POST message, you'll have to add some headers to the request.
Here's an example of doing an HTTP post using C#: stackoverflow.com/questions/4015324/http-request-with-post
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.