当前位置:首页 > Windows程序 > 正文

C# 挪用Restful 请求

2024-03-31 Windows程序

public static class HttpClientHelper { public static HttpClient GetXXXClient() { return GetHttpClient(ConfigurationManager.AppSettings["xxxxRESTService"]); } private static HttpClient GetHttpClient(string baseAddress) { var client = new HttpClient(); string authenticationSchema = "xxxxxx"; string tokenString = "xxxxx"; string host = "xxxxxx"; client.BaseAddress = new System.Uri(baseAddress); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add(authenticationSchema, tokenString); client.DefaultRequestHeaders.Host = host; return client; } }

  

       using (var client = HttpClientHelper.GetXXXXClient()) { string recordUrl = string.Format("record.json?app={0}&id=1", Utility.AppId); var response = client.GetAsync(recordUrl).Result; if (response.IsSuccessStatusCode) { var employeeInfo = response.Content.ReadAsAsync<RecordInfo>().Result; Console.WriteLine("test"); } }

  

1        Record record = new Record(); 2 record.app = "182"; 3 Dictionary<string, FieldValue> dicFields = new Dictionary<string, FieldValue>(); 4 dicFields.Add("Id", new FieldValue() { value = "123" }); 5 6 using (var client = HttpClientHelper.GetKintoneClient()) 7 { 8 var response = client.PostAsync<object>("record.json", record, new JsonMediaTypeFormatter()).Result; 9 if (response.IsSuccessStatusCode) 10 { 11 var content = response.Content.ReadAsAsync<ResponseRecord>().Result; 12 if (content != null) 13 { 14 Console.WriteLine("success"); 15 } 16 } 17 }

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/35863.html

Jm-杰米博客Jamie
草根站长的技术交流乐园!IT不会不要紧快来好好学习吧!
  • 20786文章总数
  • 7494590访问次数
  • 建站天数
  • 友情链接