Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get json from MVC4 C# with no javascript and no Ajax

I get the feeling I really should be learning WCF for this (feel free to comment if you agree), but, I want to query a website and get a result back, in either XML or JSON format.

In this case, I'm choosing JSON.

I have a controller in a web site (www.site1.com) , which looks like

public JsonResult Save(bool willSave)
{
   //logic with the parameters to go here
   return Json(new { code = 200, description = "OK" }, JsonRequestBehavior.AllowGet);
}

Now, I'd like to get this information from another website, so in www.site2.com I have nothing... I have no idea what code I can write, simply because all of the examples I've seen where you query json uses javascript/Ajax.

I don't want to use JavaScript or Ajax (I know how to do that), for this project I'm trying to do everything I can server side.

I'd like to be able to do the following

public ActionResult Do() 
{
    var json = someHowQuerySite1.com?withQueryString=true;//THIS IS THE ISSUE
    var model = CreateModel(json);
    return View(model);
}

As you can hopefully see,

var json = someHowQuerySite1.com?withQueryString=true;//THIS IS THE ISSUE

I don't know what syntax to write here.

like image 667
MyDaftQuestions Avatar asked Dec 24 '22 19:12

MyDaftQuestions


1 Answers

The simplest way, replace

var json = someHowQuerySite1.com?withQueryString=true;

with

using (var client = new HttpClient())
{
    var responseString = client.GetStringAsync("http://www.example.com/recepticle.aspx?withQueryString=true");

    var json = myJsonUtililty.toJson(responseString);
}

HTTP request with post

like image 161
AmmarCSE Avatar answered Dec 27 '22 08:12

AmmarCSE