Skip to content

Instantly share code, notes, and snippets.

@ryan-nauman
Created July 5, 2012 19:16
Show Gist options
  • Save ryan-nauman/3055803 to your computer and use it in GitHub Desktop.
Save ryan-nauman/3055803 to your computer and use it in GitHub Desktop.
Poor c#/js proxy
function jsonProxyDo(jsonDataModel, callback, errorback) {
$.ajax({
type: "POST",
dataType: "json",
url: settings.jsonProxyAddress,
data: $.param({ 'd': JSON.stringify({
'Data': JSON.stringify(jsonDataModel),
'BaseAddress': settings.endpoint,
'ServiceName': 'SomeService.svc/js/',
'MethodName': 'SomeMethod' })
}),
success: function (msg) {
callback(msg);
},
error: function (err) {
errorback(err);
}
});
};
public class ProxyRequest
{
public string BaseAddress { get; set; }
public string ServiceName { get; set; }
public string MethodName { get; set; }
public string Type { get; set; }
public string Data { get; set; }
}
public partial class JsonProxy : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string data = Request.Form["d"];
if (!string.IsNullOrEmpty(data))
{
ProxyRequest r = JsonConvert.DeserializeObject<ProxyRequest>(data);
HttpWebRequest request = null;
if (r.Type.Equals("POST"))
{
request = (HttpWebRequest)WebRequest.Create(string.Format("{0}/{1}/{2}", r.BaseAddress, r.ServiceName, r.MethodName));
request.Method = r.Type;
Stream reqStream = request.GetRequestStream();
byte[] reqBytes = Encoding.UTF8.GetBytes(r.Data);
reqStream.Write(reqBytes, 0, reqBytes.Length);
reqStream.Close();
}
if (r.Type.Equals("GET"))
{
request.Method = r.Type;
request = (HttpWebRequest)WebRequest.Create(string.Format("{0}/{1}/{2}?{3}", r.BaseAddress, r.ServiceName, r.MethodName, r.Data));
}
request.ContentType = "application/json";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader contentReader = new StreamReader(response.GetResponseStream());
Response.ContentType = response.ContentType;
Response.Write(contentReader.ReadToEnd());
}
else
{
Response.Write("Must provide JSON to use this page.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment