HttpClientResponse辅助类

        /// <summary>
        /// POST方式请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postData"></param>
        /// <returns></returns>
        public static string PostResponse(string url, string postData)
        {
            
            string result = string.Empty;
            //设置Http的正文
            //HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(postData));
            HttpContent httpContent = new StringContent(postData);
            //设置Http的内容标头
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            //设置Http的内容标头的字符
            httpContent.Headers.ContentType.CharSet = "utf-8";
            using (HttpClient httpClient = new HttpClient())
            {
                //异步Post
                HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
                //确保Http响应成功
                if (response.IsSuccessStatusCode)
                {
                    //异步读取json
                    result = response.Content.ReadAsStringAsync().Result;
                }
            }
            WriteLogFile("地址:" + url + "
内容:" + postData+"
结果:"+ result);
            return result;
        }
        /// <summary>
        /// GET方式请求
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string Gethttp(string url)
        {
            string result = string.Empty;
            using (HttpClient httpClient = new HttpClient())
            {
                //异步Post
                HttpResponseMessage response = httpClient.GetAsync(url).Result;
                //确保Http响应成功
                if (response.IsSuccessStatusCode)
                {
                    //异步读取json
                    result = response.Content.ReadAsStringAsync().Result;
                }
            }
            return result;
        }
        /// <summary>
        /// 写入日志文件
        /// </summary>
        /// <param name="input"></param>
        public static void WriteLogFile(string input)
        {

            string getPath = System.Web.HttpContext.Current.Server.MapPath("~/logs/");
            if (!Directory.Exists(getPath))
                Directory.CreateDirectory(getPath);
            ///指定日志文件的目录
            string fname = getPath +
            DateTime.Now.Year + '-' +
            DateTime.Now.Month + '-' +
            DateTime.Now.Day + "_LogFile" + ".txt";

            if (!File.Exists(fname))
            {
                //不存在文件
                File.Create(fname).Dispose();//创建该文件
            }

            /**/
            ///判断文件是否存在以及是否大于2K
            /* if (finfo.Length > 1024 * 1024 * 10)
            {
            /**/
            //文件超过10MB则重命名
            /* File.Move(fname, Directory.GetCurrentDirectory() + DateTime.Now.TimeOfDay + "\LogFile.txt");
            //删除该文件
            //finfo.Delete();
            }*/

            using (StreamWriter log = new StreamWriter(fname, true))
            {
                //FileStream fs = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);FileMode.Append

                ///设置写数据流的起始位置为文件流的末尾
                log.BaseStream.Seek(0, SeekOrigin.End);

                ///写入“Log Entry : ”
                log.Write("

Log Entry : ");

                ///写入当前系统时间并换行
                log.Write("{0} {1} 

", DateTime.Now.ToLongTimeString(),
                DateTime.Now.ToLongDateString());

                ///写入日志内容并换行
                log.Write(input + "

");

                //清空缓冲区
                log.Flush();
                //关闭流
                log.Close();
            }
        }



        /// <summary>
        /// 将异常打印到LOG文件
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="LogAddress">日志文件地址</param>
        public static void WriteLogTxt(Exception ex, string LogAddress = "")
        {
            //如果日志文件为空,则默认在Debug目录下新建 YYYY-mm-dd_Log.log文件
            if (LogAddress == "")
            {
                ///指定日志文件的目录
                LogAddress = "D:\" +
                DateTime.Now.Year + '-' +
                DateTime.Now.Month + '-' +
                DateTime.Now.Day + "_Log.log";
            }

            FileInfo finfo = new FileInfo(LogAddress);

            if (!finfo.Exists)
            {
                FileStream fs;
                fs = File.Create(LogAddress);
                fs.Close();
                finfo = new FileInfo(LogAddress);
            }

            //把异常信息输出到文件
            StreamWriter sw = new StreamWriter(LogAddress, true);
            sw.WriteLine("当前时间:" + DateTime.Now.ToString());
            sw.WriteLine("异常信息:" + ex.Message);
            sw.WriteLine("异常对象:" + ex.Source);
            sw.WriteLine("调用堆栈:
" + ex.StackTrace.Trim());
            sw.WriteLine("触发方法:" + ex.TargetSite);
            sw.WriteLine();
            sw.Close();
        }
原文地址:https://www.cnblogs.com/s666/p/14317786.html