static void Main(string[] args) { using (var http = new HttpClient()) { SendSms sms = new SendSms { Msg = "hello", PhoneNum = "188" }; RestTemplate rest = new RestTemplate(http); rest.ConsulServerUrl = "http://127.0.0.1:8500"; var result = rest.PostAsync("http://MsgService/api/SMS/Send_MI", sms).Result; Console.WriteLine(result.StatusCode); var getResult = rest.GetForEntityAsync <Product[]>("http://ProductService/api/Product").Result; foreach (var g in getResult.Body) { Console.WriteLine(g.Id + " " + g.Name); } var getResult2 = rest.GetForEntityAsync <Product>("http://ProductService/api/Product/1").Result; Console.WriteLine(getResult2.Body.Name); } Console.ReadKey(); }
private static void Main(string[] args) { using (HttpClient httpClient = new HttpClient()) { RestTemplate rest = new RestTemplate(httpClient); Console.WriteLine("---查询数据---------"); var ret1 = rest.GetForEntityAsync <Product[]>("http://ProductService/api/Product/").Result; Console.WriteLine(ret1.StatusCode); if (ret1.StatusCode == System.Net.HttpStatusCode.OK) { foreach (var p in ret1.Body) { Console.WriteLine($"id={p.Id},name={p.Name}"); } } Console.WriteLine("---新增数据---------"); Product newP = new Product(); newP.Id = 888; newP.Name = "辛增"; newP.Price = 88.8; var ret = rest.PostAsync("http://ProductService/api/Product/", newP).Result; Console.WriteLine(ret.StatusCode); } Console.ReadKey(); }
/// <summary> /// RestTemplateCore 方式 /// </summary> /// <returns></returns> static async Task RestTemplateCoreAsync() { using (HttpClient httpClient = new HttpClient()) { RestTemplate rest = new RestTemplate(httpClient); Console.WriteLine("---查询数据---------"); var resp1 = await rest.GetForEntityAsync <Product[]>("http://ProductService/api/Product"); Console.WriteLine(resp1.StatusCode); if (resp1.StatusCode == System.Net.HttpStatusCode.OK) { foreach (var p in resp1.Body) { Console.WriteLine($"id={p.Id},name={p.Name}"); } } Console.WriteLine("---新增数据---------"); Product newP = new Product(); newP.Id = 888; newP.Name = "辛增"; newP.Price = 88.8; var resp2 = await rest.PostAsync("http://ProductService/api/Product", newP); Console.WriteLine(resp2.StatusCode); } }
static void Main(string[] args) { using (HttpClient httpClient = new HttpClient()) { RestTemplate rest = new RestTemplate(httpClient, "http://127.0.0.1:8500"); Console.WriteLine("---querying---------"); var headers = new HttpRequestMessage().Headers; headers.Add("aa", "666"); var ret1 = rest.GetForEntityAsync <Product[]>("http://ProductService/api/Product/", headers).Result; Console.WriteLine(ret1.StatusCode); if (ret1.StatusCode == System.Net.HttpStatusCode.OK) { foreach (var p in ret1.Body) { Console.WriteLine($"id={p.Id},name={p.Name}"); } } Console.WriteLine("---add new---------"); Product newP = new Product(); newP.Id = 888; newP.Name = "xinzeng"; newP.Price = 88.8; var ret = rest.PostAsync("http://ProductService/api/Product/", newP).Result; Console.WriteLine(ret.StatusCode); } Console.ReadKey(); }
private static void UseRestTemplateWay() { var restTemplate = new RestTemplate(new HttpClient()) { ConsulServerUrl = "http://127.0.0.1:8500" }; var result = restTemplate.PostAsync("http://MessageServce/api/Message/Send", new SmsMessage { To = "13312345678", Content = "hi" }).Result; Console.WriteLine(result.StatusCode); }
public virtual async Task Send_HW(string phoneNum, string msg) { SendSMSReq req = new SendSMSReq(); req.phoneNum = phoneNum; req.msg = msg; using (var http = new HttpClient()) { RestTemplate rest = new RestTemplate(http); var resp = await rest.PostAsync("http://MsgService/api/SMS/Send_HW", req); if (resp.StatusCode != System.Net.HttpStatusCode.OK) { throw new Exception("发送错误,状态码" + resp.StatusCode); } } }
private static void Demo1() { using (HttpClient http = new HttpClient()) { RestTemplate rest = new RestTemplate(http) { ConsulServerUrl = "http://127.0.0.1:8500" }; SendSms sms = new SendSms { msg = "您好,欢迎", phoneNum = "189189" }; var resp = rest.PostAsync("http://msgservice/api/SMS/Send_MI", sms).Result; Console.WriteLine(resp.StatusCode); } }