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