• 以POST方式推送JSON数据,并接收返回的服务器消息


      private static string GetResult(string jsonString, string type)
            {
                string url = GetUrl(type);
                string result = "";
                string data = "jsonStr=" + jsonString;//这个是传入的参数格式大致为要求参数名  比如此方法要求Json的这个参数名为data 
                System.Net.ServicePointManager.DefaultConnectionLimit = 200;//设置线程个数
                //todo:考虑记录日志的方法,这里日志建议放入专用的记日志文件夹
                byte[] postData = Encoding.UTF8.GetBytes(data);
                System.GC.Collect();//先设置垃圾回收,防止因request资源过多产生超时现象
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;// url为接口地址,建议传入type后通过方法获取
                request.Method = "POST";
                request.KeepAlive = false;
                request.AllowAutoRedirect = true;
                request.ContentType = "application/x-www-form-urlencoded";
                request.UserAgent =
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR  3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
                //request.ContentLength = postData.Length;
                request.Timeout = 300000;
                request.ServicePoint.Expect100Continue = false;
                // 提交请求数据  
                Stream outputStream = request.GetRequestStream();
                outputStream.Write(postData, 0, postData.Length);
                outputStream.Close();
                //获取返回值 
                StreamReader reader;
                var response = request.GetResponse() as HttpWebResponse;
                var responseStream = response.GetResponseStream();
                responseStream.ReadTimeout = 300000;//设置读取时间
                reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
                string srcString = "";
                try
                {
                    //流的读取方式
                    srcString = reader.ReadToEnd();
                    result = srcString; //返回值赋值 
                    //todo:考虑记录日志的方法
                }
                catch (IOException ex)
                {
                    throw ex;//todo:考虑记录日志的方法
                }
                finally
                {
                    if (responseStream != null)
                    {
                        responseStream.Close();
                        responseStream = null;
                    }
                    if (reader != null)
                    {
                        reader.Close();
                    }
                    if (response != null)
                    {
                        response.Close();
                    }
                    if (request != null)
                    {
                        request.Abort();
                    }
                }
                return result;
            }
  • 相关阅读:
    tuple元组
    list列表
    OS模块
    time模块/datetime模块/calendar模块
    Codeforces Round #196 (Div. 2)
    【HDU 2853】 KM算法
    【HDU1914 The Stable Marriage Problem】稳定婚姻问题
    【HDU4585 Shaolin】map的经典运用
    【HDU4578 Transformation】线段树
    【HDU4632 Palindrome subsequence】区间dp
  • 原文地址:https://www.cnblogs.com/Nicolasap/p/5604425.html
Copyright © 2020-2023  润新知