C# 有错误日志返回的POST 请求 WebException

 /// <summary>
        /// POST请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="value"></param>
        /// <param name="contentType"></param>
        /// <param name="mediaType"></param>
        /// <returns></returns>
        public static string HttpPost(string url, string value, string contentType = "application/json", string mediaType = "json")
        {

            try
            {
                string result = "";
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                httpWebRequest.Timeout = 120000;
                httpWebRequest.Method = "POST";
                
                httpWebRequest.ContentType = contentType;
                if (!string.IsNullOrEmpty(mediaType))
                {
                    httpWebRequest.MediaType = mediaType;
                }
                byte[] btBodys = Encoding.UTF8.GetBytes(value);
                httpWebRequest.ContentLength = btBodys.Length;
                httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
                
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

                StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
                result = streamReader.ReadToEnd();

                httpWebResponse.Close();
                streamReader.Close();
                httpWebRequest.Abort();
                httpWebResponse.Close();
                return result;
            }
            catch (WebException ex)
            {
                HttpWebResponse res = (HttpWebResponse)ex.Response;
                Stream myResponseStream = res.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                string retString = myStreamReader.ReadToEnd();
            }
            return "";
        }

C# 有错误日志返回的POST 请求 WebException

上一篇:spark streaming之 windowDuration、slideDuration、batchDuration?


下一篇:Winform 窗体自适应