Esempio n. 1
0
        /// <summary>
        /// get请求
        /// </summary>
        /// <typeparam name="T">返回值类型</typeparam>
        /// <param name="url">请求url</param>
        /// <param name="methodName">方法名字,记录日志</param>
        /// <returns>结果</returns>
        public static T Get <T>(string url, string methodName = "Get")
        {
            DateTime startTime = DateTime.Now;
            string   result    = string.Empty;

            try
            {
                if (url.StartsWith("https"))
                {
                    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                }

                HttpClient client = new HttpClient();
                client.Timeout = new TimeSpan(0, 1, 0);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage responseMessage = client.GetAsync(url).Result;
                responseMessage.EnsureSuccessStatusCode();
                result = responseMessage.Content.ReadAsStringAsync().Result;
                if (typeof(T) == typeof(string))
                {
                    return((T)Convert.ChangeType(result, typeof(T)));
                }

                return(JosnHelper.DeserializeObject <T>(result));
            }
            finally
            {
                WriteLog(url, methodName, startTime, string.Empty, result);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 以text/json提交
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="url">请求地址</param>
        /// <param name="json">入参</param>
        /// <param name="methodName">方法名,记录日志</param>
        /// <returns>结果</returns>
        public static T PostForm <T>(string url, string json, string methodName = "PostForm")
        {
            DateTime startTime = DateTime.Now;
            string   result    = string.Empty;

            try
            {
                if (url.StartsWith("https"))
                {
                    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                }
                HttpClient client = new HttpClient();
                client.Timeout = new TimeSpan(0, 1, 0);
                HttpContent httpContent = new StringContent(json);
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
                HttpResponseMessage responseMessage = client.PostAsync(url, httpContent).Result;
                responseMessage.EnsureSuccessStatusCode();
                result = responseMessage.Content.ReadAsStringAsync().Result;
                if (typeof(T) == typeof(string))
                {
                    return((T)Convert.ChangeType(result, typeof(T)));
                }
                return(JosnHelper.DeserializeObject <T>(result));
            }
            finally
            {
                WriteLog(url, methodName, startTime, json, result);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// jsonpost请求
        /// </summary>
        /// <typeparam name="T">返回结果</typeparam>
        /// <param name="url">请求地址</param>
        /// <param name="json">入参</param>
        /// <param name="methodName">方法名,记录日志</param>
        /// <param name="headers">请求头</param>
        /// <returns>结果</returns>
        public static T PostJsonWithHeader <T>(string url, string json, Dictionary <string, string> headers, string methodName = "PostJson")
        {
            DateTime startTime = DateTime.Now;
            string   result    = string.Empty;

            try
            {
                if (url.StartsWith("https"))
                {
                    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                }
                HttpClient client = new HttpClient();
                client.Timeout = new TimeSpan(0, 1, 0);
                HttpContent httpContent = new StringContent(json);
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                if (headers != null && headers.Count > 0)
                {
                    foreach (var item in headers.Keys)
                    {
                        client.DefaultRequestHeaders.Add(item, headers[item]);
                    }
                }

                HttpResponseMessage responseMessage = client.PostAsync(url, httpContent).Result;
                responseMessage.EnsureSuccessStatusCode();
                result = responseMessage.Content.ReadAsStringAsync().Result;
                if (typeof(T) == typeof(string))
                {
                    return((T)Convert.ChangeType(result, typeof(T)));
                }
                return(JosnHelper.DeserializeObject <T>(result));
            }
            finally
            {
                WriteLog(url, methodName, startTime, $"{json}{Environment.NewLine}{JosnHelper.SerializeObject(headers)}", result);
            }
        }