Hi All,
I want to call a Rest service url through MVC can any one guide me how to call.
Thnaks in advance
Srujana
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Ramesh MaruthiRank #937 · 2.2K reputationPosted Sep 4, 2014, 6:29 AM EDT
http://codebetter.com/johnvpetersen/2012/03/22/accessing-a-asp-net-webapi-rest-service-from-asp-net-mvc/
http://blackriver.to/2011/09/rest-service-with-asp-net-mvc-part-1/
Sample code :
class Program
{
static void Main()
{
RunAsync().Wait();
}
static async Task RunAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:9000/"); // here mention ur Url as http://localhost:7083/esbGlobalServices/cacheRefresh
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// HTTP GET
HttpResponseMessage response = await client.GetAsync("api/products/1");
if (response.IsSuccessStatusCode)
{
Product product = await response.Content.ReadAsAsync
Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);
}
}
}
}
}
Jignesh TrivediRank #16 · 50.3K reputationPosted Sep 4, 2014, 7:51 PM EDT
Hi,
there are many way to consume REST services in MVC application like you can consume by Jquery, by server side code....
Please refer
http://www.codeproject.com/Articles/233572/Build-truly-RESTful-API-and-website-using-same-ASP
http://api.jquery.com/jquery.getjson/
http://www.codeproject.com/Articles/664157/Consume-RESTful-Service-using-jQuery-in-Simple-S
hope this will help you.
Guest UserRank #259 · 9.8K reputationPosted Sep 4, 2014, 3:23 AM EDT
refer this example: private void GetPOSTResponse(Uri uri, string data, Action callback) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); request.Method = "POST"; request.ContentType = "text/plain;charset=utf-8"; System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); byte[] bytes = encoding.GetBytes(data); request.ContentLength = bytes.Length; using (Stream requestStream = request.GetRequestStream()) { // Send the data. requestStream.Write(bytes, 0, bytes.Length); } request.BeginGetResponse((x) => { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(x)) { if (callback != null) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response)); callback(ser.ReadObject(response.GetResponseStream()) as Response); } } }, null); }srujana thallam67 reputationPosted Sep 4, 2014, 3:32 AM EDT
I want to call this url::http://localhost:7083/esbGlobalServices/cacheRefresh
Ho w i write code to call this.please tell me