Skip to content

Instantly share code, notes, and snippets.

@ryankirkman
Created May 30, 2012 03:51
Show Gist options
  • Save ryankirkman/2833646 to your computer and use it in GitHub Desktop.
Save ryankirkman/2833646 to your computer and use it in GitHub Desktop.
ProxyRequest proxies a request to a JSON-based API to avoid the cross origin request issue.
// ProxyRequest proxies a request to a JSON-based API
// to avoid the cross origin request issue.
// It assumes the API supports POST.
// JsonResult is an ASP.NET MVC construct.
private JsonResult ProxyRequest(string url, string data)
{
HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(url);
wr.Method = "POST";
wr.ContentType = "application/json";
// Set the data to send.
using (var streamWriter = new StreamWriter(wr.GetRequestStream()))
{
streamWriter.Write(data);
}
// Get the response.
var httpResponse = (HttpWebResponse)wr.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
JsonResult jr = new JsonResult();
jr.Data = streamReader.ReadToEnd();
return jr;
}
}
@thomasdavis
Copy link

Gi joe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment