rickythefox rickythefox - 6 months ago 37
Ajax Question

How to build a ASPX proxy page for JSONP calls

I have a page on the local intranet that provides information in JSONP format and want external users to be able to use that page for AJAX calls.

For this I want to write an ASPX proxy page that passes the client request to the internal page (on another server) and then sends the unaltered response to the external client.

What would be the easiest way to accomplish this?

Answer

Solved this using the following code in Page_Load:

        var request = (HttpWebRequest)WebRequest.Create("http://jsonsource/");
        var response = (HttpWebResponse) request.GetResponse();
        var json = new StreamReader(response.GetResponseStream()).ReadToEnd();

        Response.ClearHeaders();
        Response.ClearContent();
        Response.Clear();
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/json";
        Response.ContentEncoding = Encoding.UTF8;
        Response.Write(json);
        Response.Flush();