zoukankan      html  css  js  c++  java
  • .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;
            }
        }
  • 相关阅读:
    Android中的IMEI
    《JAVA与模式》之适配器模式(转)
    海量日志数据__怎么在海量数据中找出重复次数最多的一个
    Java中的IO流系统详解(转载)
    获取网络文件长度问题
    内存泄漏
    Ubuntu12.04不能连接小米开发(转)
    Java/C++中数组的区别
    Android批量插入数据到SQLite数据库
    泛型编程 基础
  • 原文地址:https://www.cnblogs.com/GoCircle/p/9708218.html
Copyright © 2011-2022 走看看