分享最实用的技术,创造更大的价值

HttpWebRequest HttpWebResponse 返回大量数据无法接收解决方案

HttpWebRequest 、HttpWebResponse返回内容太多时,基础连接已经关闭: 接收时发生错误。经过多种情况分析排查后,解决方案参考如下

                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";

                webReq.Accept = "*/*";
                webReq.KeepAlive = false; //重点注意此参数
                webReq.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);

                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);
                newStream.Close();

                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string _Result = sr.ReadToEnd();



联系
QQ
电话
咨询电话:189-8199-7898
TOP