-
-
Save mikaelo/6932751 to your computer and use it in GitHub Desktop.
This file contains 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
// 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment