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 "";
            }

  • 相关阅读:
    Windows 程序员必备的知识和工具
    由级别和性格特征将程序员分类 看看你属于哪一种
    调试九法:软硬件错误的排查之道<书评>
    逆向反汇编代码推算C++的局部变量
    逆向分析一个完整的C++程序包含寄存器与参数传递详解
    1607:Unable to install InstallShield Scripting runtime
    Form的构造函数和Load事件的区别?
    "春运男子持刀强行劫走17张卧铺票" ....
    SQL SERVER 2005中的Case When用法
    SBO中各个版本的密码
  • 原文地址:https://www.cnblogs.com/LuoEast/p/14271171.html
Copyright © 2011-2022 走看看