[英]How to get json from MVC4 C# with no javascript and no Ajax
我覺得我真的應該為此學習WCF(如果你同意的話可以隨意評論),但是,我想以XML或JSON格式查詢網站並獲得結果。
在這種情況下,我選擇JSON。
我在網站(www.site1.com)上有一個控制器,看起來像
public JsonResult Save(bool willSave)
{
//logic with the parameters to go here
return Json(new { code = 200, description = "OK" }, JsonRequestBehavior.AllowGet);
}
現在,我想從另一個網站獲取這些信息,所以在www.site2.com我什么都沒有...我不知道我能編寫什么代碼,只是因為我看到你查詢的所有例子json使用javascript / Ajax。
我不想使用JavaScript或Ajax(我知道該怎么做),對於這個項目,我正在努力做我可以服務器端的一切。
我希望能夠做到以下幾點
public ActionResult Do()
{
var json = someHowQuerySite1.com?withQueryString=true;//THIS IS THE ISSUE
var model = CreateModel(json);
return View(model);
}
正如你所希望看到的,
var json = someHowQuerySite1.com?withQueryString=true;//THIS IS THE ISSUE
我不知道在這里寫什么語法。
最簡單的方法,替換
var json = someHowQuerySite1.com?withQueryString=true;
同
using (var client = new HttpClient())
{
var responseString = client.GetStringAsync("http://www.example.com/recepticle.aspx?withQueryString=true");
var json = myJsonUtililty.toJson(responseString);
}
您想使用HttpWebRequest來請求www.site1.com/Save?save=true。 就像是
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://[urlhere]");
HttpWebResponse response = request.GetResponse();
using(Stream responsestream = response.GetResponseStream())
{
//Get your JSON from the stream here
}
使用這樣的東西
var response = client.PostAsJsonAsync("UserApi/ValidateUserLogin", new UsersBLL { Username = userName, UserPassword = password }).Result;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.