• .net APIHelper client获取数据


    using Newtonsoft.Json;
    using System.Net.Http.Headers;
    
        public static class APIHepler
        {
            public static string Get(string url)
            {
                HttpClient client = new HttpClient();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
                return client.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
            }
    
            public static string Post(string url, object data)
            {
                HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(data));
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                HttpClient client = new HttpClient();
    
                return client.PostAsync(url, httpContent).Result.Content.ReadAsStringAsync().Result;
            }
    
            public static string Get(string httpUrl, ref List<string> msg, string source)
            {
                string strRet = string.Empty;
                try
                {
                    using (var client = new HttpClient())
                    {
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                        HttpResponseMessage response = client.GetAsync(httpUrl).Result;
                        if (response.IsSuccessStatusCode)
                        {
                            strRet = response.Content.ReadAsStringAsync().Result;
                        }
                    }
                }
                catch (Exception ex)
                {
                    strRet = string.Empty;
                    //日志记录
                }
                return strRet;
            }
            public static string Post(string httpUrl, string requestJson, ref List<string> msg, string source)
            {
                string strRet = string.Empty;
                try
                {
                    using (var client = new HttpClient())
                    {
                        HttpContent httpContent = new StringContent(requestJson);
                        httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                        HttpResponseMessage response = client.PostAsync(httpUrl, httpContent).Result;
                        if (response.IsSuccessStatusCode)
                        {
                            strRet = response.Content.ReadAsStringAsync().Result;
                        }
                    }
                }
                catch (Exception ex)
                {
                    strRet = string.Empty;
                    //日志记录
                }
                return strRet;
            }
        }
  • 相关阅读:
    Python_代码练习_写一个判断是否为小数的函数
    Python学习杂记_11_函数(一)
    Python学习杂记_10_三元运算符
    Python学习杂记_9_集合操作
    Python学习杂记_8_从程序外部传参的办法sys.argv
    Python学习杂记_7_文件操作
    Python学习杂记_6_字典常用操作
    Python学习杂记_5_列表常用操作
    Python学习杂记_4_分支和循环
    Python学习杂记_3_字符串操作的常用方法
  • 原文地址:https://www.cnblogs.com/GoCircle/p/9708218.html
Copyright © 2020-2023  润新知