Created
October 30, 2012 20:20
-
-
Save jkresner/3982746 to your computer and use it in GitHub Desktop.
Asp .net Mvc 4 Proxy Server/Controller (For help with Cross Domain Request)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WebApplication : System.Web.HttpApplication | |
{ | |
protected void Application_Start() | |
{ | |
RouteTable.Routes.MapRoute("HttpProxy", "proxy/{*path}", new { controller = "Proxy", action = "Http" }) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.su = {} # declare app namespace | |
#We only need proxy for IE as other browsers support CORS | |
su.initServerUrl = (url) -> | |
if $.browser.msie && parseInt($.browser.version, 10) < 10 | |
su.serverUrl = '/proxy' | |
else | |
su.serverUrl = url # e.g. http://api.mydataserver.com |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProxyController : System.Web.Mvc.Controller | |
{ | |
protected string svcUrl = WebConfigurationManager.AppSettings["SvcUrl"]; | |
public ActionResult Http() | |
{ | |
return Content(grabContent(HttpContext.Request.Url.PathAndQuery.Replace("/proxy",""))); | |
} | |
/// <see>http://stackoverflow.com/questions/3447589/copying-http-request-inputstream</see> | |
private string grabContent(string url) | |
{ | |
string content; | |
// Create a request for the URL. | |
var req = HttpWebRequest.Create(svcUrl+url); | |
req.Method = HttpContext.Request.HttpMethod; | |
//-- No need to copy input stream for GET (actually it would throw an exception) | |
if (req.Method != "GET") | |
{ | |
req.ContentType = "application/json"; | |
Request.InputStream.Position = 0; //***** THIS IS REALLY IMPORTANT GOTCHA | |
var requestStream = HttpContext.Request.InputStream; | |
Stream webStream = null; | |
try | |
{ | |
//copy incoming request body to outgoing request | |
if (requestStream != null && requestStream.Length > 0) | |
{ | |
req.ContentLength = requestStream.Length; | |
webStream = req.GetRequestStream(); | |
requestStream.CopyTo(webStream); | |
} | |
} | |
finally | |
{ | |
if (null != webStream) | |
{ | |
webStream.Flush(); | |
webStream.Close(); | |
} | |
} | |
} | |
// If required by the server, set the credentials. | |
req.Credentials = CredentialCache.DefaultCredentials; | |
// No more ProtocolViolationException! | |
using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) | |
{ | |
// Display the status. | |
//Console.WriteLine(response.StatusDescription); | |
// Get the stream containing content returned by the server. | |
using (Stream dataStream = response.GetResponseStream()) | |
{ | |
// Open the stream using a StreamReader for easy access. | |
StreamReader reader = new StreamReader(dataStream); | |
// Read the content. | |
content = reader.ReadToEnd(); | |
} | |
} | |
return content; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
+1 to the add headers
+1 to add response content type
+1 to decoding URL
+1 to request content-type not hardcoded.
Thanks @FennNaten !!