zoukankan      html  css  js  c++  java
  • 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 "";
            }

  • 相关阅读:
    洛谷 P1195 口袋的天空
    洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver
    Bzoj3277 串
    Bzoj1312 / POJ3155 Neerc2006 Hard Life
    Bzoj2655 calc
    51Nod 1228 序列求和
    洛谷P2901 [USACO08MAR]牛慢跑Cow Jogging
    Bzoj1042 [HAOI2008]硬币购物
    Bzoj3884 上帝与集合的正确用法
    Bzoj4161 Shlw loves matrixI
  • 原文地址:https://www.cnblogs.com/LuoEast/p/14271171.html
Copyright © 2011-2022 走看看