Skip to content Skip to sidebar Skip to footer

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.

Solution 1:

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

Solution 2:

You want to use a HttpWebRequest to request www.site1.com/Save?save=true. Something like

HttpWebRequestrequest= (HttpWebRequest)WebRequest.Create("http://[urlhere]");
HttpWebResponseresponse= request.GetResponse();
using(Streamresponsestream= response.GetResponseStream())
{
 //Get your JSON from the stream here
}

Solution 3:

Use something like this

var response = client.PostAsJsonAsync("UserApi/ValidateUserLogin", new UsersBLL { Username = userName, UserPassword = password }).Result;

Post a Comment for "How To Get Json From Mvc4 C# With No Javascript And No Ajax"