C# 调用Restful 请求

2021-07-20 11:16

阅读:597

标签:head   host   att   appid   null   .post   uri   type   stat   

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().Result;

                    Console.WriteLine("test");
                }
            }

  

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

 

C# 调用Restful 请求

标签:head   host   att   appid   null   .post   uri   type   stat   

原文地址:http://www.cnblogs.com/KruceCoder/p/8040505.html


评论


亲,登录后才可以留言!