转载,(无法从传输中读取数据 )
c#文章好找啊,记录一下,参考解决地址:https://www.cnblogs.com/LL-723/p/4268020.html
直接贴代码
string responseContent = "";
int count = 0;
Stream responseStream;
string restring;
try
{
System.GC.Collect();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "PROPFIND";
request.KeepAlive=false;
ServicePointManager.DefaultConnectionLimit = 500;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
lock (sign)
{
//using (Stream resStream = response.GetResponseStream())
//{
// using (StreamReader reader = new StreamReader(resStream, Encoding.UTF8))
// {
// responseContent = reader.ReadToEnd();
// }
//}
//restring = responseContent;
//方式1
responseStream = response.GetResponseStream();
MemoryStream ms = new MemoryStream();
responseStream.CopyTo(ms);
byte[] bt = ms.ToArray();
restring = System.Text.Encoding.UTF8.GetString(bt);
ms.Close();
responseStream.Close();
request.Abort();
request = null;
response.Close();
response=null;
}
}
catch (Exception ex)
{}
其中内存流方式
responseStream = response.GetResponseStream();
MemoryStream ms = new MemoryStream();
responseStream.CopyTo(ms);
byte[] bt = ms.ToArray();
restring = System.Text.Encoding.UTF8.GetString(bt);
能解决无法从传输连接中读取数据: 远程主机强迫关闭了一个现有的连接的问题
————————————————
版权声明:本文为CSDN博主「菜鸟王小凡」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wqs15192095633/article/details/109122811