报错:
请求被中止 连接被意外关闭。
================================================================
//发起请求 HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(urlGet); wbRequest.Method = "POST"; wbRequest.ContentType = "application/json;charset=UTF-8"; byte[] load = Encoding.UTF8.GetBytes(paramJson); wbRequest.ContentLength = load.Length; using (Stream writer = wbRequest.GetRequestStream()) { writer.Write(load, 0, load.Length); } //接收返回包 string resultJson = string.Empty; HttpWebResponse wbResponse = (HttpWebResponse)(await wbRequest.GetResponseAsync()); MemoryStream stream = new MemoryStream(); using (Stream responseStream = wbResponse.GetResponseStream()) { } return stream; } catch (Exception ex) { Log4NetHelper.Log(LogTypeEnum.ServicesLog, LogLevelEnum.Error, currentMethodLog + "接口报错", ex); throw ex; }
================================================================
因为使用了using,如果直接返回Stream,出了using范围再使用这个Stream就会报错。
接收外网流的时候,这个Stream会一直使用连接,连接一旦断开,这个流就不会存在了。
所以后面再使用这个Steam的时候,就会报错:请求被中止 连接被意外关闭。
解决办法:在using内部,拿到这个流的时候,就将流转换为byte[],byte[]就有值了,和外网流已经无关系了,即使外网流断开,我这个byte[]已经拿到值。后面再次使用的时候就不会报错。
================================================================
转换为byte[]的代码:
MemoryStream stream = new MemoryStream(); //将数据流转为byte[] List<byte> bytes = new List<byte>(); int temp = responseStream.ReadByte(); while (temp != -1) { bytes.Add((byte)temp); temp = responseStream.ReadByte(); } byte[] mg = bytes.ToArray(); stream = new MemoryStream(mg);
================================================================
//接收返回包 HttpWebResponse wbResponse = (HttpWebResponse)(await wbRequest.GetResponseAsync()); MemoryStream stream = new MemoryStream(); using (Stream responseStream = wbResponse.GetResponseStream()) {
一个返回的流只能使用一次,第二次使用虽然有值,但是值是无效的,生成不了图片。
如果我有两个流,Stream A 和 Stream B,我想要用外网接收的流WebStream都给A,B赋值,直接赋值后,A和B都有值了,但是使用了A之后,B就会直接失效,或者使用B之后,A就会直接失效。
错误代码展示:
//接收返回包 HttpWebResponse wbResponse = (HttpWebResponse)(await wbRequest.GetResponseAsync()); Stream streamSuccess = new MemoryStream(); Stream streamFAIL = new MemoryStream(); using (Stream responseStream = wbResponse.GetResponseStream()) { streamSuccess = responseStream; streamFAIL = responseStream; }
这样的代码,使用了A之后,B就会失效。
我想要做到的是,WebStream给A,B赋值,A和B都有值了,并且使用A之后,B不会失效。怎么做呢?就是赋值给A,B之前,先将WebStream转换为byte[]字节,再将byte[]分别给A和B赋值,这样赋值后,使用了A并不会导致B失效。
具体实现代码:
//接收返回包 HttpWebResponse wbResponse = (HttpWebResponse)(await wbRequest.GetResponseAsync()); Stream streamSuccess = new MemoryStream(); Stream streamFAIL = new MemoryStream(); using (Stream responseStream = wbResponse.GetResponseStream()) { //将数据流转为byte[] List<byte> bytes = new List<byte>(); int temp = responseStream.ReadByte(); while (temp != -1) { bytes.Add((byte)temp); temp = responseStream.ReadByte(); } byte[] mg = bytes.ToArray(); streamSuccess = new MemoryStream(mg); streamFAIL = new MemoryStream(mg);
这样, streamSuccess 和 streamFAIL 都有值了,并且使用streamSuccess并不会导致 streamFAIL 失效。
================================================================
结束。
================================================================